|
From: Luke D. <cod...@ho...> - 2003-02-03 05:16:13
|
----- Original Message -----
From: "pcotts" <pc...@wc...>
To: <min...@li...>
Sent: Monday, February 03, 2003 6:23 AM
Subject: [Mingw-users] Creating a dynamic loadable dll
> i am trying to create a dll on my Windows98 box. I have downloaded and
install
> MinG// Video.h - Definition of interface IVideo W, and set my path
variable to
> C:\Mingw\bin. Listed below is my source code:
>
> //video.h
> #ifdef BUILD_DLL
> #define EXPORT __declspec(dllexport)
> #else
> #define EXPORT __declspec(dllimport)
> #endif
>
> class EXPORT IGeneral
You don't need to export any classes when you are using this COM-like
technique, but you may have other reasons for doing so.
[snip]
>
> extern "C" IGeneral* _stdcall CreateVCR();
This function needs to be exported.
> //File tv.cpp
>
> #include "Video.h"
> #include <windows.h>
> #include <iostream.h>
>
> void UseVideo(IVideo* pVideo);
> void UseSVideo(ISVideo* pSVideo);
>
> IGeneral* CreateInstance(char* pszDll)
> {
> typedef IGeneral* (_stdcall *CREATEVCRPROC)(void);
>
> HINSTANCE h = LoadLibrary(pszDll);
> CREATEVCRPROC proc =
> reinterpret_cast<CREATEVCRPROC>(GetProcAddress(h, "CreateVCR"));
You should always check the return value of LoadLibrary and GetProcAddress.
The crash was because GetProcAddress returns NULL because it can't find the
function. Apart from forgetting to export it, by default the function will
be exported as "CreateVCR@0", and you can use the "pexports" or "objdump -p"
command to check this. So either you pass GetProcAddress this string, or use
"-Wl,--kill-at" when you build to DLL so that it will be exported without
the @0 (this way is more like MSVC I think).
> return (*proc)();
> }
>
[snip]
>
> With this code I am trying to selectively import the dll of my choose
using
> LoadLibrary. I compile my code ass follows:
>
> g++ -o tv.o -c tv.cpp
This command is incorrect because you need to use "g++ -c" to compile
without linking, otherwise an executable named "tv.o" will be created. This
command is also redundant because you don't use tv.o later on.
> g++ -o vcr.dll -DBUILD_DLL=1 -shared tv.cpp
Perhaps this was just a typo in the email, but this command should be
compiling vcr.cpp, not tv.cpp. The rest is correct, but you may wish to
add -Wl,--kill-at.
> g++ -o tv.exe tv.cpp -L./ -lvcr
Since you are using LoadLibrary you don't need to link with the DLL, but it
shouldn't cause any problems if you do.
>
> this code dose compile and link but when run I obtain a system error of
> proforming an illegal operation. If any one can help thank you I would
> apreciate it.
>
> Sincerly,
> Distraught over C++ dlls
If this is your first question on the list, you are much closer to the
correct solution than most people usually are :-). In fact, your fixed code
could be a useful example to other people that are trying to use C++ DLLs
with different compilers.
Luke
|