I want to export a function in DLL, and using extern C and __stdcall
together to resolve name decoration issue -- so I need
dllexport/stdcall/extern C together to define a function.
But I have played a while there is always compile error? Why? Here is my
code and related compile error.
[Code]
extern "C" __declspec(dllexport) __stdcall BOOL WINAPI StoreData(DWORD dw)
1>d:\visual studio 2008\projects\testdll\testdll\dllmain.cpp(78) : error
C2143: syntax error : missing ';' before '__stdcall'
1>d:\visual studio 2008\projects\testdll\testdll\dllmain.cpp(78) : warning
C4229: anachronism used : modifiers on data are ignored
1>d:\visual studio 2008\projects\testdll\testdll\dllmain.cpp(78) : error
C4430: missing type specifier - int assumed. Note: C++ does not support
default-int
1>d:\visual studio 2008\projects\testdll\testdll\dllmain.cpp(78) : error
C2377: 'BOOL' : redefinition; typedef cannot be overloaded with any other
symbol
1> c:\program files\microsoft
sdks\windows\v6.0a\include\windef.h(153) : see declaration of 'BOOL'
1>d:\visual studio 2008\projects\testdll\testdll\dllmain.cpp(79) : error
C4430: missing type specifier - int assumed. Note: C++ does not support
default-int
1>d:\visual studio 2008\projects\testdll\testdll\dllmain.cpp(105) : error
C2143: syntax error : missing ';' before '__stdcall'
1>d:\visual studio 2008\projects\testdll\testdll\dllmain.cpp(105) : error
C4430: missing type specifier - int assumed. Note: C++ does not support
default-int
1>d:\visual studio 2008\projects\testdll\testdll\dllmain.cpp(106) : error
C4430: missing type specifier - int assumed. Note: C++ does not support
default-int
[/Code]
regards,
George
> I want to export a function in DLL, and using extern C and __stdcall
> together to resolve name decoration issue
__stdcall is not for name decoration issues, __stdcall does decorate
exported identifiers.
__stdcall is for stack management issues (especially with old VB6 clients).
If you don't want name decorations, you should use .def files.
> But I have played a while there is always compile error? Why? Here is my
> code and related compile error.
>
> [Code]
> extern "C" __declspec(dllexport) __stdcall BOOL WINAPI StoreData(DWORD dw)
Try:
extern "C" BOOL __stdcall StoreData( DWORD dw );
and use a .def file.
I think that WINAPI is a macro that expands to __stdcall (which is the
calling convention used by Win32 API functions).
So you should not duplicate __stdcall and WINAPI on the same declaration.
Moreoever, __stdcall must be put between <return-type> and <function-name>,
as you can read from __stdcall documentation on MSDN:
http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx
Giovanni
You are correct and I have tried your solution could compile.
A further question, I am confused about the MSDN page content "The __stdcall
calling convention is used to call Win32 API functions. The callee cleans the
stack, so the compiler makes vararg functions __cdecl." -- does it mean for
__stdcall caller cleans the stack, but for __cdecl call callee cleans the
stack?
regards,
George
> Thanks Giovanni,
You are welcome.
> A further question, I am confused about the MSDN page content "The
> __stdcall
> calling convention is used to call Win32 API functions. The callee cleans
> the
> stack, so the compiler makes vararg functions __cdecl." -- does it mean
> for
> __stdcall caller cleans the stack, but for __cdecl call callee cleans the
> stack?
I encourage you to go and read __cdecl description on MSDN, and to compare
"Stack-maintenance responsibility" entry in the summary table for both
__stdcall and __cdecl:
__stdcall
http://msdn.microsoft.com/en-us/library/zxk0tw93.aspx
__cdecl
http://msdn.microsoft.com/en-us/library/zkwh89ks.aspx
Stack-maintenance responsibility
__stdcall: Called function pops its own arguments from the stack.
__cdecl: Calling function pops the arguments from the stack
Giovanni
Question answered.
regards,
George
regards,
George
A quick search through the include files would show you that WINAPI is
defined like this:
#define WINAPI __stdcall
So, you have __stdcall in there twice. If you remove the first one, you'll
find that it compiles correctly.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.
You are correct!
regards,
George