DLL help

Hi Guys,

So after googling for a couple of days I have resorted to asking for help here.
Basically, I have followed MSDN tutorial on DLL "http://msdn.microsoft.com/en-us/library/ms235636.aspx" and I have managed to get it running without any problems.

My first question though is, how is the dll being used? I see the functions are exported so that it is available to external applications for use.

But I thought, to be able to use it too... you need an "_declspec(dllimport)" declaration too.

I don't see that declaration anywhere in the code, hence my question how is it being used?

The other question I had is if I have a source file only (no header file)... and I build a dll from it? Can I use the dll. At the moment I can't seem to be able to use the dll.

This DLL is pretty confusing to learn.
Thanks guys!
Last edited on
declspec(dllimport) is not strictly necessary
This might help clear up the confusion or not - as the case may be.

http://msdn.microsoft.com/en-us/library/aa271769%28v=vs.60%29.aspx
You have some possibilities to load dll:
statically with lib: and then you need "_declspec(dllimport)
dynamically with "LoadLibrary", GetProcAddress": and then you don't need it.

you need the header file for the import statement and function declarations(if you use it statically).

if you use it dynamically, you still need the declaration for creating function pointer for GetProcAddress
closed account (DSLq5Di1)
When you add a reference to the DLL project, Visual Studio will link in the .LIB file produced from that project and import declarations from the header into your main project.

Without the header, you'll need to provide the declarations yourself, with _declspec(dllimport). Without a .LIB file, you need to link at runtime using LoadLibrary()/GetProcAddress() as mentioned above.
Thx for replies guys... I will need some time to digest this information.

In the meantime, I am practicing with my own DLL code.
What I have is very simply...

Header file "MyAdd.h"
1
2
3
4
5
6
#include <string>
class MyAdd
{
public:
	extern "C" _declspec(dllexport) int add( int a, int b );
};


Source file "MyAdd.cpp"
1
2
3
4
int MyAdd::add( int a, int b )
{
	return a + b;
}


The error I am getting is...
"...\desktop\dlltutorial\dlltutorial\myadd.h(7): error C2059: syntax error : 'string'"

Now, from what I have been told.. it is because extern "C" can only be used with C++, not with C.

I am confused though... because this is C++ that I am doing, not C. Therefore, I don't understand why...

Can you guys tell me why?
Thx again guys.
Last edited on
The code you posted doesn't have 'string'. Can you please post all the relevant code?

Don't export members of a class. Always export the whole class.
Last edited on
I forgot to copy and paste "#include <string>" but my code does have it.
I have updated the code above now.

Can you please post all the relevant code?

That is all the code... it is purely just practice & learning code.
closed account (DSLq5Di1)
Where is the usage of string in that code? the error message points to line 7 of myadd.h, but you've only shown us 6 lines.. and the syntax error is not among them.

Are you forgetting the namespace declaration in the offending line? std::string
MyAdd.h Header
1
2
3
4
5
6
7
#include <string>

class MyAdd
{
	public:
		extern "C" _declspec(dllexport) int add( int a, int b );
};


MyAdd.cpp Implementation
1
2
3
4
5
6
#include "MyAdd.h"

int MyAdd::add( int a, int b )
{
	return a + b;
}


1>------ Build started: Project: DLLTutorial, Configuration: Debug Win32 ------
1>Build started 9/09/2012 8:03:49 PM.
1>InitializeBuildStatus:
1> Touching "Debug\DLLTutorial.unsuccessfulbuild".
1>ClCompile:
1> MyAdd.cpp
1>...\desktop\dlltutorial\dlltutorial\myadd.h(6): error C2059: syntax error : 'string'
1>...\desktop\dlltutorial\dlltutorial\myadd.h(6): error C2238: unexpected token(s) preceding ';'
1>...\desktop\dlltutorial\dlltutorial\myadd.cpp(3): error C2039: 'add' : is not a member of 'MyAdd'
1>...\desktop\dlltutorial\dlltutorial\myadd.h(4) : see declaration of 'MyAdd'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.21
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


That is the code and compile error copied line for line from my project (using MS Visual Studio 2010)... it is really that simple, there are no other codes.

If I comment out
extern "C"
then it compiles without issues.

1>------ Build started: Project: DLLTutorial, Configuration: Debug Win32 ------
1>Build started 9/09/2012 8:09:04 PM.
1>InitializeBuildStatus:
1> Creating "Debug\DLLTutorial.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> MyAdd.cpp
1>Link:
1>...\Desktop\DLLTutorial\Debug\DLLTutorial.lib and object ...\Desktop\DLLTutorial\Debug\DLLTutorial.exp
1>LinkEmbedManifest:
1>...\Desktop\DLLTutorial\Debug\DLLTutorial.lib and object ...\Desktop\DLLTutorial\Debug\DLLTutorial.exp
1>...\Desktop\DLLTutorial\Debug\DLLTutorial.dll
1>FinalizeBuildStatus:
1> Deleting file "Debug\DLLTutorial.unsuccessfulbuild".
1> Touching "Debug\DLLTutorial.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:00.69
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


But I want the extern "C" to prevent C++ from mangling my functions.

Where is the usage of string in that code? the error message points to line 7 of myadd.h, but you've only shown us 6 lines.. and the syntax error is not among them.

Are you forgetting the namespace declaration in the offending line? std::string

I just didn't align it 100% accurate before... but that is not the issue. I am 100% sure the issue is with external "C".

Thanks for the help thus far guys, I appreciate it!
closed account (DSLq5Di1)
Ooh you're right, sorry! I was blinded by the error message.

extern "C" will not function at class scope. Do you really need C linkage? name mangling isn't an issue if you link implicitly. There are some workarounds.. but the code can get messy, and has it's own caveats:-

http://eli.thegreenplace.net/2011/09/16/exporting-c-classes-from-a-dll/
http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL
http://www.codeguru.com/cpp/w-p/dll/importexportissues/article.php/c123/Explicitly-Linking-to-Classes-in-DLLs.htm
Hi guys,
http://eli.thegreenplace.net/2011/09/16/exporting-c-classes-from-a-dll/
Has extra stuff that I don't need... all I want is to be able to compile the above, I don't need all the bells and whistles mentioned in the article.

http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL
Has interesting comparison of the different approaches, but doesn't quite have the answer I am seeking.

http://www.codeguru.com/cpp/w-p/dll/importexportissues/article.php/c123/Explicitly-Linking-to-Classes-in-DLLs.htm
Discusses explicit linking (Just trying to learn implicit linking for now) and other concepts too which I don't need for what the above code is doing.

Well, after reading those articles, they all seem to have 1 common topic, factory functions or vtables, are you saying my code will not compile unless I incorporate these concepts into my code?

The other possibility is to use a .def file to make it compatible with C... but from all the code I've seen, read and tried they don't seem with have issues with compiling when they have "extern "C"" in their examples... so it is really infuriating that I am when my code is so simple/useless.

Any ideas?
Hey guys,

Wondering if you can help me...

I have this particular piece of code in 2 of my source codes
1
2
3
4
BOOL WINAPI DllEntryPoint( HANDLE hInstance, WORD wDataSeg, WORD cbHeapSize, LPSTR lpCmdLine )
{
	return (1);
}


Now the compile error I am getting says that it is already defined... which I understand.

But because this is code written by someone else and I am updating it I don't want to take out anything that I don't need to.

If I leave 1 of them in and comment out the other then it compiles no problems.

So my question is how important is that function?
Topic archived. No new replies allowed.