2012-07-25 73 views
0

我編譯了下面的代碼,它在我的C代碼中用#include <strsafe.h>聲明,並且在DEV-C++中出現以下編譯器錯誤。我想在編譯器中應該有一些選項來解決這個問題。有人可以幫助解決這個問題嗎?下面DEV-C++中的編譯器錯誤

的是從MSDN網站把我的示例代碼:

#include <windows.h> 
#include <strsafe.h> 

void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code 

    LPVOID lpMsgBuf; 
    LPVOID lpDisplayBuf; 
    DWORD dw = GetLastError(); 

    FormatMessage(
     FORMAT_MESSAGE_ALLOCATE_BUFFER | 
     FORMAT_MESSAGE_FROM_SYSTEM | 
     FORMAT_MESSAGE_IGNORE_INSERTS, 
     NULL, 
     dw, 
     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 
     (LPTSTR) &lpMsgBuf, 
     0, NULL); 

    // Display the error message and exit the process 

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
     (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
     LocalSize(lpDisplayBuf)/sizeof(TCHAR), 
     TEXT("%s failed with error %d: %s"), 
     lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf); 
    LocalFree(lpDisplayBuf); 
    ExitProcess(dw); 
} 

void main() 
{ 
    // Generate an error 

    if(!GetProcessId(NULL)) 
     ErrorExit(TEXT("GetProcessId")); 
} 

從巨大的一套錯誤的錯誤示例。

154 26 C:\Documents and Settings\[email protected]\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'char' 
155 26 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const' 
156 1 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__nullterminated' 
156 33 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before '*' token 
157 26 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const' 
158 26 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const' 
164 38 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const' 
165 38 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const' 
166 38 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] expected '=', ',', ';', 'asm' or '__attribute__' before 'const' 
173 10 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name 'STRSAFE_PCNZCH' 
174 10 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__in_range' 
175 15 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__deref_out_range' 
179 10 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name 'STRSAFE_PCNZWCH' 
180 10 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__in_range' 
181 15 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__deref_out_range' 
193 5 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__deref_in_opt_out' 
194 17 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__deref_out_range' 
200 5 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__deref_in_opt_out' 
201 17 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name '__deref_out_range' 
207 30 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name 'STRSAFE_PCNZCH' 
213 30 C:\Documents and Settings\mkumar\Desktop\DEVC\TEST-install-Driver-Difx\TO-DO\strsafe.h 
[Error] unknown type name 'STRSAFE_LPCSTR' 
continues....... 

strsafe.h文件從我收到上述錯誤,其中A部分:

// 
// These typedefs are used in places where the string is guaranteed to 
// be null terminated. 
// 
typedef __nullterminated char* STRSAFE_LPSTR; 
typedef __nullterminated const char* STRSAFE_LPCSTR; 
typedef __nullterminated wchar_t* STRSAFE_LPWSTR; 
typedef __nullterminated const wchar_t* STRSAFE_LPCWSTR; 
typedef __nullterminated const wchar_t UNALIGNED* STRSAFE_LPCUWSTR; 

// 
// These typedefs are used in places where the string is NOT guaranteed to 
// be null terminated. 
// 
typedef __possibly_notnullterminated const char* STRSAFE_PCNZCH; 
typedef __possibly_notnullterminated const wchar_t* STRSAFE_PCNZWCH; 
typedef __possibly_notnullterminated const wchar_t UNALIGNED* STRSAFE_PCUNZWCH; 


// prototypes for the worker functions 

STRSAFEWORKERAPI 
StringLengthWorkerA(
    __in STRSAFE_PCNZCH psz, 
    __in __in_range(<=, STRSAFE_MAX_CCH) size_t cchMax, 
    __out_opt __deref_out_range(<, cchMax) size_t* pcchLength); 

STRSAFEWORKERAPI 
StringLengthWorkerW(
    __in STRSAFE_PCNZWCH psz, 
    __in __in_range(<=, STRSAFE_MAX_CCH) size_t cchMax, 
    __out_opt __deref_out_range(<, cchMax) size_t* pcchLength); 

#ifdef ALIGNMENT_MACHINE 
STRSAFEWORKERAPI 
UnalignedStringLengthWorkerW(
    __in STRSAFE_PCUNZWCH psz, 
    __in __in_range(<=, STRSAFE_MAX_CCH) size_t cchMax, 
    __out_opt __deref_out_range(<, cchMax) size_t* pcchLength); 
#endif // ALIGNMENT_MACHINE 

STRSAFEWORKERAPI 
StringExValidateSrcA(
    __deref_in_opt_out STRSAFE_LPCSTR* ppszSrc, 
    __inout_opt __deref_out_range(<, cchMax) size_t* pcchToRead, 
    __in const size_t cchMax, 
    __in DWORD dwFlags); 
+4

如果您發佈了違規代碼,它可能會有所幫助。 – dandan78 2012-07-25 11:03:53

+0

考慮編寫[SSCE](http://sscce.org/),並編輯您的問題以包含代碼。 – 2012-07-25 11:05:10

+0

唯一的錯誤是與strsafe.h。 – 2vision2 2012-07-25 11:18:34

回答

2

__nullterminated,__out_opt,__in,__in_opt等這樣的字眼是Microsoft特定的關鍵字。當您使用gcc時,您可以將它們定義爲空宏,如

#define __in 
#define __deref_out_range(A,B) 
... and so on 

將這些定義插入到includes之前。

或者作爲意見建議避免使用<strsafe.h>

編輯:

MinGW的(64位來自http://equation.com建)編譯此代碼上面靜靜地(甚至strsafe.h)只是一個簡單的命令:

gcc -c Test.cpp -o Test.exe -lstdc++ 
+0

你的意思是我想定義所有這些宏?並沒有其他方式來避免這種情況,如使用一些編譯器選項?並抱歉,要求這個沒有在他們說的避免使用strsafe.h的評論..請建議.. – 2vision2 2012-07-26 03:57:55

+0

我只提出解決方案爲我工作(我使用DirectX 11頭與MinGW/GCC - 有大量的__in_opt和東東)。順便說一下,我用MinGW檢查了你的代碼,並且它靜靜地編譯。 – 2012-07-26 06:50:01

+0

是的,謝謝你。如果可能的話,我可以知道用於編譯的命令嗎?因爲我的devcon只在內部呼叫Mingw。 – 2vision2 2012-07-26 07:06:10