DllImportAttribute - anonymous usage not allowed error



By ganton ~ July 22nd, 2008. Filed under: WinForms.

Today, I ran into a strange error

error C3094: ‘System::Runtime::InteropServices::DllImportAttribute’: anonymous usage not allowed

and why this error is strange for me? I had an old dll written in managed C++ which imports a method from user32.dll. Initially, this Dll is written using VS 2005 with .Net Framwork 2.0. An the time of development and using it under VS 2005 all compiled fine. Today, I opened it using VS 2008 with .Net Framework 2.0 and I ran into this error which was not presented with VS 2005. The code snippet is


[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
extern "C" BOOL UpdateLayeredWindow(HWND hWnd, HDC hDcDst, POINT *pPtDst, SIZE *pSize, HDC hDcSrc, POINT *pPtSrc, COLORREF crKey, BLENDFUNCTION *pBlend, DWORD dwFlags);

the code is placed out of the class. Unfortunately, I do not found the reason of this error for now but when I changed importing the method a bit it compiled fine. I placed the import snippet into the class and change it a bit as below


public ref class MyClass
{
private:
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
static BOOL UpdateLayeredWindow(HWND hWnd, HDC hDcDst, POINT *pPtDst, SIZE *pSize, HDC hDcSrc, POINT *pPtSrc, COLORREF crKey, BLENDFUNCTION *pBlend, DWORD dwFlags);

}

Actually, I do not remember while it was done like in the first snippet but the second variant also is acceptable for my case. I was not able to find out what is the problem with first snippet and VS 2008 and why it doesn’t compile. If somebody has an idea add a comment, please!

Leave a Reply