How do I link to a DLL?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I link to a DLL?

James Moody
I've just started working with msys/mingw, please forgive if this is
answered elsewhere (I looked - I really did!).

I have a device, a Windows DLL for this device, and an ansi-c header
file (which the company considers a complete enough API). I already have
the device driver installed on my Windows system. I'd like to use
msys/mingw to write a small test program (open the device, read some
registers, set some configuration bits, then exit cleanly).

How do I get gcc or g++ to use the DLL file as the library? Is there a
special flag on the compile line? Or do I have to create a library from
the DLL?

I can't believe I'm the first person to ask this, but I could not find
answers or examples anywhere (or maybe I just did not understand what I
found!).

Thanks.
--Jim







------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: How do I link to a DLL?

John Klehm
On Thu, Aug 27, 2009 at 6:22 AM, James Moody<[hidden email]> wrote:
> How do I get gcc or g++ to use the DLL file as the library? Is there a
> special flag on the compile line? Or do I have to create a library from
> the DLL?
>

You use a set of system calls to access the dll, GetModuleHandle (or
LoadLibrary if youre not sure the dll has been loaded once already)

Then you call GetProcAddress using the handle you got from one of the
above calls and the function signature you want to use from your dlls
header.

MSDN has a reasonable example:
http://msdn.microsoft.com/en-us/library/ms683212(VS.85).aspx

Note you should call FreeLibrary at some point if you go the LoadLibrary route.


Hope this helps,
--John

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: How do I link to a DLL?

Tuomo Latto
John Klehm wrote:

> On Thu, Aug 27, 2009 at 6:22 AM, James Moody<email-address-certain-someone-feels-should-be-shown> wrote:
>> How do I get gcc or g++ to use the DLL file as the library? Is there a
>> special flag on the compile line? Or do I have to create a library from
>> the DLL?
>
> You use a set of system calls to access the dll, GetModuleHandle (or
> LoadLibrary if youre not sure the dll has been loaded once already)
>
> Then you call GetProcAddress using the handle you got from one of the
> above calls and the function signature you want to use from your dlls
> header.
>
> MSDN has a reasonable example:
> http://msdn.microsoft.com/en-us/library/ms683212(VS.85).aspx
>
> Note you should call FreeLibrary at some point if you go the LoadLibrary route.

I think the OP means normal linking:
http://www.mingw.org/wiki/Specify_the_libraries_for_the_linker_to_use
(via http://www.mingw.org/wiki/FAQ#toc4 "How do I...")

You may also need to search for information on
import libraries and .def files if you get linking errors.


--
Tuomo

... Features should be discovered, not documented


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: How do I link to a DLL?

Norman Bauer
In reply to this post by James Moody
Jim if you find the answer I sure would like to know too. I asked a
similar question last week and have still been searching for the
answer.

Norm

On 8/27/09, James Moody <[hidden email]> wrote:

> I've just started working with msys/mingw, please forgive if this is
> answered elsewhere (I looked - I really did!).
>
> I have a device, a Windows DLL for this device, and an ansi-c header
> file (which the company considers a complete enough API). I already have
> the device driver installed on my Windows system. I'd like to use
> msys/mingw to write a small test program (open the device, read some
> registers, set some configuration bits, then exit cleanly).
>
> How do I get gcc or g++ to use the DLL file as the library? Is there a
> special flag on the compile line? Or do I have to create a library from
> the DLL?
>
> I can't believe I'm the first person to ask this, but I could not find
> answers or examples anywhere (or maybe I just did not understand what I
> found!).
>
> Thanks.
> --Jim
>
>
>
>
>
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus
> on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.
>
> Most annoying abuses are:
> 1) Top posting
> 2) Thread hijacking
> 3) HTML/MIME encoded mail
> 4) Improper quoting
> 5) Improper trimming
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
>

--
Sent from my mobile device

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: How do I link to a DLL?

Red Rackham
In reply to this post by James Moody
I have been lucky enough to survive this process, writing a DLL (which you already have) and writing a GUI to use access it.  You would want to create a separate class (header and C++ files) for abstracting your DLL.  Then your main module can instantiate the DLL "wrapper" or interface.  Here is my code for interfacing to my DLL, which you can modify for your own use:

//======== Include File ========//

#ifndef FX2DLL_H
#define FX2DLL_H
// This module is a wrapper for interfacing to my FX2 EEPROM DLL
#include "windows.h"
#define FX2DLL_PATH  "C:\\Program Files\\msfx2\\fx2eep.dll"
typedef struct VID_PID
{
    UINT vid;
    UINT pid;
};

// define dll function prototypes
typedef UINT (__cdecl *FX2DLL_INIT)(VOID);              // DLL Init Function
typedef CHAR *(__cdecl *FX2DLL_GET_PROBE_NAME)(VOID);   // DLL GetProbeName Function
typedef VOID (__cdecl *FX2DLL_SELECT_PROBE)(UCHAR);     // DLL SelectProbe Function
typedef UCHAR *(__cdecl *FX2DLL_GET_BUFFER)(VOID);      // DLL GetBuffer Function
typedef VOID (__cdecl *FX2DLL_WRITE_BLOCK)(UCHAR);      // DLL WriteBlock Function
typedef VOID (__cdecl *FX2DLL_READ_BLOCK)(UCHAR);       // DLL ReadBlock Function
typedef VOID (__cdecl *FX2DLL_WRITE_VID_PID)(VID_PID*); // DLL WriteVidPid Function
typedef VOID (__cdecl *FX2DLL_READ_VID_PID)(VID_PID*);  // DLL ReadVidPid Function
typedef VOID (__cdecl *FX2DLL_WRITE_SERIAL)(CHAR *);    // DLL WriteSerial Function
typedef CHAR *(__cdecl *FX2DLL_READ_SERIAL)(VOID);      // DLL ReadSerial Function
class Fx2Dll
{
    private:
        // declare dll functions
        FX2DLL_INIT             Fx2DllInit;
        FX2DLL_GET_PROBE_NAME   Fx2DllGetProbeName;
        FX2DLL_SELECT_PROBE     Fx2DllSelectProbe;
        FX2DLL_GET_BUFFER       Fx2DllGetBuffer;
        FX2DLL_WRITE_BLOCK      Fx2DllWriteBlock;
        FX2DLL_READ_BLOCK       Fx2DllReadBlock;
        FX2DLL_WRITE_VID_PID    Fx2DllWriteVidPid;
        FX2DLL_READ_VID_PID     Fx2DllReadVidPid;
        FX2DLL_WRITE_SERIAL     Fx2DllWriteSerial;
        FX2DLL_READ_SERIAL      Fx2DllReadSerial;
    public:
        Fx2Dll();
        ~Fx2Dll();
        CHAR *fx2dll_path;
        CHAR wcpSerial[80];
        BOOL fFreeResult;
        BOOL fRunTimeLinkSuccess;
        HINSTANCE hFx2Dll;
        // declare local functions connected to dll functions
        // try to make callable function names the same as in dll
        UINT Init();
        CHAR *GetProbeName();
        VOID SelectProbe(UCHAR);
        UCHAR *GetBuffer();
        VOID WriteBlock(UCHAR);
        VOID ReadBlock(UCHAR);
        VOID WriteVidPid(VID_PID *);
        VOID ReadVidPid(VID_PID *);
        VOID WriteSerial(CHAR *);
        CHAR *ReadSerial();

};

#endif // FX2DLL_H

// ====== CPP File =======//

#include "fx2dll.h"
Fx2Dll::Fx2Dll()
{
    fFreeResult = FALSE;
    fRunTimeLinkSuccess = FALSE;
    hFx2Dll = LoadLibrary( TEXT(FX2DLL_PATH) );
    if (hFx2Dll != NULL)
    {
        Fx2DllInit = (FX2DLL_INIT) GetProcAddress(hFx2Dll, "Init");
        if (Fx2DllInit != NULL)
        {
            fRunTimeLinkSuccess = TRUE;
            // Connect up all the DLL function pointers
            Fx2DllGetProbeName  = (FX2DLL_GET_PROBE_NAME)   GetProcAddress(hFx2Dll, "GetProbeName");
            Fx2DllSelectProbe   = (FX2DLL_SELECT_PROBE)     GetProcAddress(hFx2Dll, "SelectProbe");
            Fx2DllGetBuffer     = (FX2DLL_GET_BUFFER)       GetProcAddress(hFx2Dll, "GetBuffer");
            Fx2DllWriteBlock    = (FX2DLL_WRITE_BLOCK)      GetProcAddress(hFx2Dll, "WriteBlock");
            Fx2DllReadBlock     = (FX2DLL_READ_BLOCK)       GetProcAddress(hFx2Dll, "ReadBlock");
            Fx2DllWriteVidPid   = (FX2DLL_WRITE_VID_PID)    GetProcAddress(hFx2Dll, "WriteVidPid");
            Fx2DllReadVidPid    = (FX2DLL_READ_VID_PID)     GetProcAddress(hFx2Dll, "ReadVidPid");
            Fx2DllWriteSerial   = (FX2DLL_WRITE_SERIAL)     GetProcAddress(hFx2Dll, "WriteSerial");
            Fx2DllReadSerial    = (FX2DLL_READ_SERIAL)      GetProcAddress(hFx2Dll, "ReadSerial");
        }
    }
}
Fx2Dll::~Fx2Dll()
{
    fFreeResult = FreeLibrary(hFx2Dll);
    return;
}
UINT Fx2Dll::Init()
{
    return (Fx2DllInit) ();
}
CHAR *Fx2Dll::GetProbeName()
{
    return (Fx2DllGetProbeName) ();
}
VOID Fx2Dll::SelectProbe(UCHAR prb)
{
    (Fx2DllSelectProbe) (prb);
    return;
}
UCHAR *Fx2Dll::GetBuffer()
{
    return (Fx2DllGetBuffer) ();
}
VOID Fx2Dll::WriteBlock(UCHAR block)
{
    (Fx2DllWriteBlock) (block);
    return;
}
VOID Fx2Dll::ReadBlock(UCHAR block)
{
    (Fx2DllReadBlock) (block);
    return;
}
VOID Fx2Dll::WriteVidPid(VID_PID *vp)
{
    (Fx2DllWriteVidPid) (vp);
    return;
}
VOID Fx2Dll::ReadVidPid(VID_PID *vp)
{
    return (Fx2DllReadVidPid) (vp);
}
VOID Fx2Dll::WriteSerial(CHAR *ser)
{
    (Fx2DllWriteSerial) (ser);
    return;
}
CHAR *Fx2Dll::ReadSerial()
{
    CHAR* serial = (Fx2DllReadSerial) ();
    return serial;
}

//====================//

Sorry I would have posted file somewhere but I don't have an account anywhere to host files.
To access standard Windows DLL's you don't have to go to quite as much effort, this is for a custom DLL. 



     

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: How do I link to a DLL?

James Moody
In reply to this post by Norman Bauer
I started a thread asking how to link to a previously provided DLL.

Norman Bauer wrote:
> Jim if you find the answer I sure would like to know too. I asked a
> similar question last week and have still been searching for the
> answer.
>
> Norm
>  
Here's what worked for me (compiled from info from this group and a few
other sources)

Recap: I want to use a device, the manufacturer provides only a DLL file
and a C header file.  Let's call them 'xdev.dll' and 'xdev.h'

 (1) Compile the test program that includes calls to the functions in
the DLL.  The compiler/linker will complain about each function. For
example, if I try to use a function 'xdev_Init'  I'll get something like
this:

    ... unresolved reference .... xdev_Init@4

(2) Use 'pexports' (in the 'mingw-utils' pacakge) to generate a def file:

  % pexports xdev.dll > xdev.def

(3) Edit the .def file to change the entry "xdev_Init" to "xdev_Init@4"

(4) Use dlltool to create the library. Note: the '-k' switch is important

 % dlltool -k -d xdev_def -l xdev.lib

(5) Now compile my program using the new library

 % g++ hello.d -o hello.exe -L. -l xdev

Other resources: I found the following page to be helpful. In it, the
author references a tutorial by Colin Peters (which is not directly
hosted but you can download and use it locally) which also has very good
info on '@n' and why it is used.

http://www.emmestech.com/moron_guides/moron1.html

Hope this helps
--Jim

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users