www.digitalmars.com         C & C++   DMDScript  

c++ - Compiling a DLL using DMC for use with MSVC program

reply Daniel White <twinbee42 skytopia.com> writes:
Hi all,

I have a simple function I'd like to compile as a DLL:

***********************
extern "C"
{
   __declspec(dllexport) int Add( int a, int b )   {
      return( a + b );
   }
}
***********************

Usually, I would compile that with Visual Studio C++, and then run this short
program which calls the dll (also compiled with VS):

***********************************
#include <iostream>
#include <windows.h>
typedef int (*AddFunc)(int,int);
int main() {
	HINSTANCE h = LoadLibrary("dll-testing.dll");
	AddFunc af = (AddFunc)GetProcAddress(h, "Add");
	printf("%i\n", af(23, 43));	system("PAUSE");
}
***********************************

It works. However, I need the user to be able to compile it, so I would need to
distribute the compiler with my software. There seem to many unknowns about the
Microsoft C++ compiler (including where it's even located or what it's called,
the terms and conditions, plus maybe it's very bloated?).

So my idea was to use DMC or something like it. DMC seems very portable (though
obviously I'd ask for permission first or pay if need be). However,
unfortunately, I can't get it to compile the DLL properly. I try this:

dmc dll-testing.cpp -WD

It creates the DLL (about 2k instead of 26k like the MVSC++ version). So far so
good, but then when I try to run the program which calls the DLL, I get an
error:

"dll-testing.dll is either not designed to run on Windows or it contains an
error. Try installing the program again using...... blah blah...."

Is what I want to do even possible? I'd hate to resort to a scripting language
to achieve what I want because obviously, C/C++ is so fast (I need the speed).
It seems like this issue the only thing stopping me now :)
Nov 08 2010
parent reply Bertel Brander <bertel post4.tele.dk> writes:
If you add this at the top of dll-testing.cpp:

#include <windows.h>

__declspec(dllexport) BOOL APIENTRY DllMain(HANDLE hModule, DWORD 
ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
       break;
    }
    return TRUE;
}

And compile:
dmc -mn -WD dll-testing.cpp kernel32.lib

It should work.


Den 09-11-2010 07:21, Daniel White skrev:
 Hi all,

 I have a simple function I'd like to compile as a DLL:

 ***********************
 extern "C"
 {
     __declspec(dllexport) int Add( int a, int b )   {
        return( a + b );
     }
 }
 ***********************

 Usually, I would compile that with Visual Studio C++, and then run this short
 program which calls the dll (also compiled with VS):

 ***********************************
 #include<iostream>
 #include<windows.h>
 typedef int (*AddFunc)(int,int);
 int main() {
 	HINSTANCE h = LoadLibrary("dll-testing.dll");
 	AddFunc af = (AddFunc)GetProcAddress(h, "Add");
 	printf("%i\n", af(23, 43));	system("PAUSE");
 }
 ***********************************

 It works. However, I need the user to be able to compile it, so I would need to
 distribute the compiler with my software. There seem to many unknowns about the
 Microsoft C++ compiler (including where it's even located or what it's called,
 the terms and conditions, plus maybe it's very bloated?).

 So my idea was to use DMC or something like it. DMC seems very portable (though
 obviously I'd ask for permission first or pay if need be). However,
 unfortunately, I can't get it to compile the DLL properly. I try this:

 dmc dll-testing.cpp -WD

 It creates the DLL (about 2k instead of 26k like the MVSC++ version). So far so
 good, but then when I try to run the program which calls the DLL, I get an
 error:

 "dll-testing.dll is either not designed to run on Windows or it contains an
 error. Try installing the program again using...... blah blah...."

 Is what I want to do even possible? I'd hate to resort to a scripting language
 to achieve what I want because obviously, C/C++ is so fast (I need the speed).
 It seems like this issue the only thing stopping me now :)

Nov 09 2010
parent reply Daniel White <twinbee42 skytopia.com> writes:
Wow, thank you!! That did indeed do the trick. I have to say, I don't understand
any of it, but hopefully I can take a fire and forget approach towards this
extra
code, without worrying too much how it affects the rest...?

I also was able to simplify it a little bit by removing the -mn bit; not sure if
that will affect performance however (I use Windows 7 btw, but others may not of
course).

Just for the record, I was also able to compile both parts almost without
modification using the Tiny C compiler (TCC). Down to 2k too. I just needed to
remove the 'extern "C"' part for TCC to work, though maybe D's apparent 'cruft'
in this case is better in terms of safety or flexibility...
Nov 09 2010
parent reply Bertel Brander <bertel post4.tele.dk> writes:
Den 10-11-2010 01:49, Daniel White skrev:
 Wow, thank you!! That did indeed do the trick. I have to say, I don't
understand
 any of it, but hopefully I can take a fire and forget approach towards this
extra
 code, without worrying too much how it affects the rest...?

Like any program needs a main (or WinMain) will any dll need a DllMain. I don't know why VisualC++ allows you to create a DLL without. The function is called when the DLL is loaded and so on: http://msdn.microsoft.com/en-us/library/ms682583%28VS.85%29.aspx You might not need to do anything with it.
 I also was able to simplify it a little bit by removing the -mn bit; not sure
if
 that will affect performance however (I use Windows 7 btw, but others may not
of
 course).

-mn might be default for DLLs
Nov 10 2010
parent reply Daniel White <twinbee42 skytopia.com> writes:
Thanks for the extra info. I fear these next problems may not be so
surmountable however.

The Microsoft compiler has special math optimizations known as SSE2
(Streaming SIMD Extensions 2), and a 'Fast' floating point model.

From what I've been researching, SSE2 may be available in the DMC compiler
(a quick mention in the changelog at digitalmars.com), but there's no
compiler flag. I checked the site's optimization page too, and tried -o+all
as well as -o-all to no avail.

Also I've heard that the DMC compiler uses a particularly accurate floating
point model. However, for my purposes, it's more important I get a fast one,
like the one MVSC++ provides.

Without those two optimizations, the Microsoft compiler ran as slowly, but
with them, I obtained a speed increase of about 3x over DMC - not
insignificant. Any ideas?
Nov 10 2010
parent Bertel Brander <bertel post4.tele.dk> writes:
Den 11-11-2010 02:46, Daniel White skrev:
 Thanks for the extra info. I fear these next problems may not be so
 surmountable however.

 The Microsoft compiler has special math optimizations known as SSE2
 (Streaming SIMD Extensions 2), and a 'Fast' floating point model.

  From what I've been researching, SSE2 may be available in the DMC compiler
 (a quick mention in the changelog at digitalmars.com), but there's no
 compiler flag. I checked the site's optimization page too, and tried -o+all
 as well as -o-all to no avail.

 Also I've heard that the DMC compiler uses a particularly accurate floating
 point model. However, for my purposes, it's more important I get a fast one,
 like the one MVSC++ provides.

 Without those two optimizations, the Microsoft compiler ran as slowly, but
 with them, I obtained a speed increase of about 3x over DMC - not
 insignificant. Any ideas?

I have absolutely no idea.
Nov 11 2010