|
Hello everybody,
I hope anyone can help me w/ my question !!!
I am trying to create a Win32 C dll in visual C++ 2008 express edition and it is working fine but I am not able to tell visual C++ to export the appropriate functions w/ their real names (e.g. function GetCANdbClassVersion will be exported as _GetCANdbClassVersion@0).
I added the following .def file to my project but it seems to have no influence to the exported function name
LIBRARY "CANdbClass.dll"
EXPORTS
GetCANdbClassVersion
I have declared the function as follows in my project (.c and .h file):
extern "C" __declspec(dllexport) BSTR __stdcall GetCANdbClassVersion(void);
do I have to tell vc what to do w/ the .def file ?
what am I doing wrong ?
thanks in advance for your help !
ganralf
|
|
|
|
|
If you use __declspec(dllexport) you don't need a DEF file.
You can get rid of the __declspec(dllexport) and use a DEF file or
use __declspec(dllexport) and forget the DEF file.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
thanks Mark for your quick answer !
ok, I have deleted the .def file and now I am using only __declspec(dllexport) but the result dll still does not contain the "real" name.
I have defined the exported function as follows in a .cpp file
extern "C" __declspec(dllexport) BSTR __stdcall GetCANdbClassVersion(void)
{
return SysAllocStringByteLen((LPCSTR)CANDBCLASSVER, strlen((const char*)CANDBCLASSVER));
}
and the appropriate header part
extern "C" __declspec(dllexport) BSTR __stdcall GetCANdbClassVersion(void);
when I am viewing the result dll with dumpbin.exe I get the following result
Setting environment for using Microsoft Visual Studio 2008 x86 tools.
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file D:\...\CANdbClass.dll
File Type: DLL
Section contains the following exports for CANdbClass.dll
00000000 characteristics
48F1B934 time date stamp Sun Oct 12 10:45:40 2008
0.00 version
1 ordinal base
1 number of functions
1 number of names
ordinal hint RVA name
1 0 00001000 _GetCANdbClassVersion@0 = _GetCANdbClassVersion@0
Summary
1000 .data
1000 .rdata
1000 .reloc
1000 .rsrc
1000 .text
C:\Programme\Microsoft Visual Studio 9.0\VC>
is there anything I have forgotten to do ? Is it because I am using Express Edition ?
thanks for your help !
ganralf
|
|
|
|
|
__stdcall functions by default are exported with an appended underscore and
the @ sign followed by the number of arguments.
If you need to export an undecorated name, then maybe use a def file and get
rid of the __declspec(dllexport) (actually, if you leave it the DEF file export
will override it anyway). In the EXPORTS section of the DEF file,
use something like
EXPORTS
GetCANdbClassVersion
I tested this on a little Win32 DLL, which I configured to create a MAP file
at link time. In the map file I see this
4 _GetCANdbClassVersion@0
exported name: GetCANdbClassVersion
which I'm pretty sure is what you want.
You can also export both a decorated and an undecorated name, like this:
EXPORTS
GetCANdbClassVersion = _GetCANdbClassVersion@0
_GetCANdbClassVersion@0 = _GetCANdbClassVersion@0
What are your requirements?
I'm wondering why you're worried about the name decorations...
If your DLL will only be used by other modules built with Microsoft
tools, then using the __declspec(dllexport)/__declspec(dllimport)
method is the easiest way to go, since you can manage your imports/exports
right in the source code (generally at one place - where the functions are
declared). No need to worry about the decorations there.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
thanks Mark for the explanations on standard dll !
I followed your instructions (deleted the __declspec(dllexport) and included a CANdbClass.def file to the project) but after I created the dll dumpbin reported the following
C:\Programme\Microsoft Visual Studio 9.0\VC>dumpbin /EXPORTS D:\30_c_projects\CA
NdbClass\CANdbClass\Release\CANdbClass.dll
Microsoft (R) COFF/PE Dumper Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file D:\30_c_projects\CANdbClass\CANdbClass\Release\CANdbClass.dll
File Type: DLL
Summary
1000 .data
1000 .rdata
1000 .reloc
1000 .rsrc
1000 .text
C:\Programme\Microsoft Visual Studio 9.0\VC>
I think this means that the function has not been exported successfully ! Do I have to tell VC what to do w/ the .def file ? (I added it by Project->Add Existing Element...)
I also want to use the dll in VB projects where I use the following function declaration
Private Declare Function GetCANdbClassVersion Lib "D:\30_c_projects\CANdbClass\CANdbClass\Release\CANdbClass.dll" _
Alias "_GetCANdbClassVersion@0" () As String
and I only want to get rid of the Alias statement
CANdbClass.def content
LIBRARY "CANdbClass.dll"
EXPORTS
GetCANdbClassVersion
thanks for your help !
ganralf
|
|
|
|
|
ganralf wrote: I think this means that the function has not been exported successfully ! Do I have to tell VC what to do w/ the .def file ?
Are you sure your linker settings are set to use the def file?
See project properties/Linker/Input/Module Definition File
You have to put the name of your def file there!
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Also, in project properties/Linker/Debugging you can
use the Generate Map File, Map File Name, and Map Exports
settings to generate a map file - will save you the extra
step of using dumpbin.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
thanks Marc ! that was the key ! now it is working fine !!!
thanks again for your help !!!
ganralf
|
|
|
|
|
Also note that in my last post, the comments apply to __stdcall
functions only.
__cdecl functions are exported without decorations so you can use
__declspec(dllexport) and not worry about it.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hallo,
I tried to use some global vars to define some application wide settings. So I defined a group of global vars in my MyApp.cpp like this:
MyApp.h:
#pragma once
....
static bool bSetOutputToDebugWindow;
....
All my (*.cpp) files include the MyApp.h file. In all files "bSetOutputToDebugWindow" was accessable and the applicaction compiles with no error. But when I set "bSetOutputToDebugWindow" (e.g. in my Apps InitInstance-Method) to true, and tried to read the value later in debugger or function the value is false! Why????
Please help to find my error`...
|
|
|
|
|
This happens because you said it was static. That means it there is a static instance of it in every module that includes MyApp.h.
If you want the variable to be global and shared among the modules of the app then it can not be static.
|
|
|
|
|
Thanks your for your answer. But when I delete the static keyword, I get a lot of compiler errors like (...already defined in ... object).
|
|
|
|
|
Are the globals changed at run-time? If they aren't you can substitute them with defines, for example:
bool bSetOutputToDebugWindow = false;
becomes
#define SETOUTPUTTODEBUGWINDOW 0
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
I usually put the declaration (extern bool bVar; ) in the common header file and in one of the cpp files (in your case MyApp.cpp)I do create the variable ( bool bVar; ).
That guarantees that I'm using the same variable across my cpp files. but personally I try to avoid using global variables. A better approach would be static member variable.
|
|
|
|
|
You should not be, under any circumstances, declaring a variable in a header file, unless it is inside a function, class, or struct declaration. That is how you get the compile errors you were talking about when you remove the static keyword
This resource[^] may be useful to you.
|
|
|
|
|
Instead of putting as static, you can use #ifndef compiler option as follows in header file.
globals.h
+++++++++
#ifndef _myglobals_h
#define _myglobals_h
.................
declare variables
...............
#endif
-Parthi
|
|
|
|
|
You should not do that.
Instead:
(1) declare your variable as extern in the (commonly included) header file, for instance in MyApp.h:
extern bool bSetOutputToDebugWindow;
(2) define the variable only once, inside one source file, for instance MyApp.cpp (please note I did not use the static keyword):
bool bSetOutputToDebugWindow;
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Dear All
In visual C++ 2005
I want to change a CString file to char*
I used following code
But fopen needs a char* for its first parameter
Please let me know what to do
Regards
char buffer[MAX_PATH];
GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH);
CString path = buffer;
path = path.Left(path.ReverseFind('\\')+1);
path += "result.mp3";
f = fopen( path , "wb");
|
|
|
|
|
It should do.
So, what's wrong ? Do you get an error ?
CString has an inner cast Operator (LPCTSTR ) which converts the CString object to a const char* (if compiling ANSI) of const wchar_t* (if building unicode), so you should not have to do any much stuff.
And please (if you get this advice from anywhere), don't use the CString::GetBuffer() ) method at all for such a thing !!!
So, to come back to you problem, what is your problem ?
|
|
|
|
|
Hi super_ttd,
What's the problem with CString::GetBuffer()? Why dont you like it?
|
|
|
|
|
GetBuffer() is completely unnecessary when you're only reading the string
(i.e. when you only need a const pointer to the string's internal buffer).
The LPCSTR operator is safe and more efficient. GetBuffer() is for when
you need a non-const pointer.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Same ocean, different fish, same bait.
You have lunch yet?
led mike
|
|
|
|
|
led mike wrote: You have lunch yet?
Heh. I'm going to HAVE to now
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
|
But it appears that VC2005 doesn't support (LPSTR) or (LPCTSTR) operator.
isn't it?
Regards
|
|
|
|