我在C中有两个项目:
首先:
include windows.h
include stdio.h
include tchar.h
int main()
{
HANDLE hFile = CreateFile("D:\\f.txt",
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile == INVALID_HANDLE_VALUE)
_tprintf("Error: CreateFile %d\n",GetLastError());
Sleep(5000);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第二:
include windows.h
include stdio.h
include tchar.h
int main()
{
HANDLE hFile = CreateFile("D:\\f.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile == INVALID_HANDLE_VALUE)
_tprintf("Error: CreateFile %d\n",GetLastError());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第一个程序应该打开文件进行读取,同时允许其他人从中读取.第二个应该打开文件进行阅读.
当我运行程序时,第二个给我错误32(ERROR_SHARING_VIOLATION).
我认为FILE_SHARE_READ的重点是允许其他线程/进程打开一个文件,只是为了读取而不管它是否已经打开.
任何人都可以帮我解决这个问题吗?
PS如果文件是邮件,那会有什么不同吗?
据我所知,当编译程序(例如用C语言编写)时,它首先被翻译成汇编语言,然后翻译成机器语言.为什么不能(不是)跳过"汇编语言步骤"?
1: #include <windows.h>
2: int& max(int& a, int& b)
3: {
4: return a > b ? a : b;
5: }
6: int main()
7: {
8: return 0;
9: }
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2008速成版大喊:
1> e:...\main.cpp(2):错误C2062:输入'int'意外
1> e:...\main.cpp(2):错误C2062:输入'int'意外
1> e:...\main.cpp(2):错误C2059:语法错误:')'
1> e:...\main.cpp(3):错误C2143:语法错误:缺少';' 在'{'之前
1> e:...\main.cpp(3):错误C2447:'{':缺少函数头(旧式正式列表?)
如果我用stdio.h或iostream替换windows.h似乎有效(或者如果我删除它)
为什么是这样?
我正在尝试使用此命令通过inf文件安装驱动程序:
rundll32.exe setupapi,InstallHinfSection DefaultInstall 128 .\my_driver.inf
Run Code Online (Sandbox Code Playgroud)
根据MSDN(http://msdn.microsoft.com/en-us/library/aa376957%28v=vs.85%29.aspx),通过提供128作为参数,除了"设置安装的默认路径"到INF的位置.这是典型设置",安装应该(+0)不要求用户重启.但是,就我而言,它始终如此.
我究竟做错了什么?
我有以下代码:
typedef enum MyEnum{
A = 0,
B,
C,
D
} MyEnumArray[] = {A, B, C, D};
Run Code Online (Sandbox Code Playgroud)
VS 2008给了我错误
C2513:'MyEnum []':在'='之前没有声明变量
直接从a声明数组的正确方法是typedef enum什么?
我有一个简单的 MFC 应用程序,我尝试在字符串末尾添加省略号。
这是我的代码:
void CMFCApplication1Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CPaintDC dc(this); // device context for painting
CRect rect;
rect.top = 0;
rect.bottom …Run Code Online (Sandbox Code Playgroud) 我用c ++创建了一个Windows服务,它创建一个事件,然后等待桌面应用程序发出信号.
这是事件创建代码(来自Windows服务):
CUpdateMonitor::CUpdateMonitor()
{
PSECURITY_DESCRIPTOR psd = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(psd, TRUE, NULL, FALSE);
SECURITY_ATTRIBUTES sa = { 0 };
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = psd;
sa.bInheritHandle = FALSE;
m_hUpdateAvailableEvent = CreateEvent(&sa, TRUE, FALSE, UPDATE_AVAILABLE_EVENT);
LocalFree(psd);
if (m_hUpdateAvailableEvent == NULL)
throw Win32Exception(GetLastError(), _T("CUpdateMonitor: Failed to create update available event"));
}
Run Code Online (Sandbox Code Playgroud)
这是桌面应用程序代码:
void CClientDlg::OnNotifyUpdate()
{
HANDLE hEvent = OpenEvent(SYNCHRONIZE, FALSE, UPDATE_AVAILABLE_EVENT); // <-- this works fine
if (hEvent == NULL)
MLOGE(_T("Could not open event. Err code: %d"), GetLastError());
else …Run Code Online (Sandbox Code Playgroud) 我有这个基类:
class BaseException
{
public:
BaseException(string _message)
{
m_message = _message;
}
string GetErrorMessage() const
{
return m_message;
}
protected:
string m_message;
};
Run Code Online (Sandbox Code Playgroud)
和这个派生类
class Win32Exception : public BaseException
{
public:
Win32Exception(string operation, int errCode, string sAdditionalInfo = "")
{
string message = "";
message += "Operation \"" + operation + "\" failed with error code ";
message += std::to_string(errCode);
if (!sAdditionalInfo.empty())
message += "\nAdditional info: " + sAdditionalInfo;
BaseException(message);
}
};
Run Code Online (Sandbox Code Playgroud)
编译器给我以下错误:
错误C2512:'BaseException':没有合适的默认构造函数可用
我知道我可以构建一个非常长的行来构造将在初始化列表中传递给基类的消息,但这种方式似乎更优雅.
我究竟做错了什么?
我有一个C++类模板和另一个继承它的类.正如您将看到的,后者不是类模板.当我尝试通过调用基类的构造函数(模板一)来定义派生类的构造函数时,会出现问题.我在代码下面发布了错误.
为简单起见,我只添加了声明.如果你觉得代码可以帮助你了解问题可能是什么,我会很乐意发布它.
state2d.h
#ifndef STATE2D_H
#define STATE2D_H
template <typename T>
class State2D
{
public:
State2D(unsigned int _rows, unsigned int _columns);
State2D(unsigned int _rows, unsigned int _columns, const T& val);
State2D(const State2D<T> &st);
~State2D();
T& operator()(unsigned int i, unsigned int j);
const T& operator()(unsigned int i, unsigned int j) const;
unsigned int GetRowCount() const;
unsigned int GetColumnCount() const;
unsigned int GetAvailablePositionsCount() const;
protected:
T** matrix;
unsigned int rows;
unsigned int columns;
unsigned int availablePositions;
};
#endif // STATE2D_H
Run Code Online (Sandbox Code Playgroud)
TicTacToeState.h
#ifndef TICTACTOESTATE_H …Run Code Online (Sandbox Code Playgroud)