Newbie - help with using .dll

K

Konx

Hi everyone.

I'm a newbie of C++ programming, but because of work I have to use it;
the problem is that I don't have the time to learn everything to fully
understand the language, so I hope you can help me with this problem.
I'll try to explain the best I can, but of course, not being an
expert, i is difficult even to explain the problem :)

Situation: I have a board with an FPGA on it. I can communicate with
the FPGA using a USB2.0 port. The people that gave me the Board, gave
me a SiUSBLib.dll and a SiUSBLib.h file and, according to them, I
should use these files to communicate with the FPGA via the USB port.
Moreover, they sent me a Driver (that I successfully installed).

Always according to their instructions this .dll is "written for the
Borland C++ Builder 6.0, but can be integrated in MSVC applications as
well". This means, for what I understand, that I can use (and I'm in
fact using) Visual C++ 2008 express edition to write my code and use
the .dll.

So, at this point, I should write some kind of code and use the
function of this .dll to work with the FPGA.

Working with a friend, we've reached this point:

in the header file (SiUSBLib.h...I repeat everything to make myself as
clear as possible) there is such a structure:

#ifdef CF__LINUX
#define DECLDIR
#endif

#ifdef __VISUALC__
#ifdef DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#elif USE_STATIC_LIB
#define DECLDIR
#else
#define DECLDIR __declspec(dllimport)
#endif
#endif

I understand that this is done to define, in some way, what kind of
system I'm using, so the header file che load the library. But, for
some kind of reason, this doesn't happen automatically, so we have to
define in our test.cpp file something like this:

#define __VISUALC__

After this, we define this line:

typedef bool DECLDIR (*InitUSBFunc)();

that honestly, is totally unclear to me. It has to do, I suppose, with
something that is written in the header file:

// USB device management functions
bool DECLDIR InitUSB();
int DECLDIR GetNumberOfUSBBoards();
int DECLDIR GetMaxNumberOfUSBBoards();
void DECLDIR * GetUSBDevice(int Id, int DevClass = -1);
void DECLDIR * GetUSBDeviceIndexed(int index);
std::stringstream DECLDIR *GetDevInfoString(int index);

Then, in the main program, we load the library with the following
commands:

HINSTANCE hinstlib; //Handle to DLL.
hinstlib = LoadLibrary(TEXT("SiUSBLib.dll"));

We found these commands surfing the web; I can't say that is totally
clear to me, but I understand that, somehow, this tell to my code:
"load the .dll, so I can use the function implemented in the .dll
itself".

Now, we are sure that the library is loaded because we have a if cycle
(for debug purpose):

InitUSBFunc _initusb;
if (hinstlib != NULL) {
printf("Library successfully LOADED\n");
}

Now, at this point, I would like to use the functions in the .dll, but
I have no idea how to do this; I tried something like:

_initusb = (InitUSBFunc) GetProcAddress(hinstlib, "InitUSB");

Because this is something I've found on the web, but it is not clear
what I'm supposed to obtain with such a statement. Moreover, if I
write something like:

if (_initusb) {
printf("true\n");
} else {
printf("false\n");
}

I always have a "false" (my guess was: I have false if I don't connect
the board, I have true if I connect the board...but it dosen't work).

Just to point out clearly what we have in the header file, there is:

class DECLDIR SiUSBDevice
{
public:
SiUSBDevice(void * dev);
~SiUSBDevice();

// update internal USB device pointer
void SetDeviceHandle(void * dev);

//Then, we have some functions:

// identification values
int GetId();
const char* GetName();
int GetClass();
int GetFWVersion();

//Or other things like:

// FPGA access (via controller core)
bool WriteXilinx (unsigned short address, unsigned char *Data, int
length = 1);
bool ReadXilinx (unsigned short address, unsigned char *Data, int
length = 1);


// FPGA configuration
bool DownloadXilinx(const char *fileName);

..
..
..
..


Now, I understand that maybe the question is really large and open,
but I can't ask nothing more specific than "can you please give some
hints and explanation of what is happening here?". I'm trying to read
a lot of documentation about API, .dll and stuff like this, but the
level is a bit too high for me.

Thank you in advance just for reading through all this stuff :)

bye

Francesco.
 
G

Gert-Jan de Vos

Hi everyone.

I'm a newbie of C++ programming, but because of work I have to use it;
the problem is that I don't have the time to learn everything to fully
understand the language, so I hope you can help me with this problem.
I'll try to explain the best I can, but of course, not being an
expert, i is difficult even to explain the problem :)

Situation: I have a board with an FPGA on it. I can communicate with
the FPGA using a USB2.0 port. The people that gave me the Board, gave
me a SiUSBLib.dll and a SiUSBLib.h file and, according to them, I
should use these files to communicate with the FPGA via the USB port.
Moreover, they sent me a Driver (that I successfully installed).

Always according to their instructions this .dll is "written for the
Borland C++ Builder 6.0, but can be integrated in MSVC applications as
well". This means, for what I understand, that I can use (and I'm in
fact using) Visual C++ 2008 express edition to write my code and use
the .dll.

So, at this point, I should write some kind of code and use the
function of this .dll to work with the FPGA.

Working with a friend, we've reached this point:

in the header file (SiUSBLib.h...I repeat everything to make myself as
clear as possible) there is such a structure:

#ifdef CF__LINUX
  #define DECLDIR
#endif

#ifdef __VISUALC__
  #ifdef DLL_EXPORT
    #define DECLDIR __declspec(dllexport)
#elif USE_STATIC_LIB
  #define DECLDIR
#else
    #define DECLDIR __declspec(dllimport)
  #endif
#endif

I understand that this is done to define, in some way, what kind of
system I'm using, so the header file che load the library. But, for
some kind of reason, this doesn't happen automatically, so we have to
define in our test.cpp file something like this:

#define __VISUALC__

This is needed to tell the header file what compiler you are using.
The DECLDIR magic is there to tell the compiler when to import or
export symbols from the dll. So far, so good.
After this, we define this line:

typedef bool DECLDIR (*InitUSBFunc)();

that honestly, is totally unclear to me. It has to do, I suppose, with
something that is written in the header file:

// USB device management functions
bool DECLDIR  InitUSB();
int  DECLDIR  GetNumberOfUSBBoards();
int  DECLDIR  GetMaxNumberOfUSBBoards();
void DECLDIR * GetUSBDevice(int Id, int DevClass = -1);
void DECLDIR * GetUSBDeviceIndexed(int index);
std::stringstream   DECLDIR *GetDevInfoString(int index);

Then, in the main program, we load the library with the following
commands:

        HINSTANCE hinstlib;    //Handle to DLL.
        hinstlib = LoadLibrary(TEXT("SiUSBLib.dll"));

Now you start to manually load the dll file and extract the symbols.
That can work but is very cumbersome as you found out. With your
library
header and dll file, there should also be a lib file that you should
add
to your linker input files. This lib file tells the linker to load the
libary symbols from the dll automatically. You should then be able to
call
the library functions normally after including the header file in your
code. A simple way to tell the linker to use that lib file is to add
this
line to your code where you do the #include "SiUSBLib.h":

#pragma comment(lib, "SiUSBLib.lib")

All of this is specific to Win32 and the microsoft compiler and has
little
to do with the C++ language. Another news group might be more
appropriate..
 
K

Konx

On 8 Gen, 11:44, Gert-Jan de Vos <[email protected]>
wrote:

[cut]
All of this is specific to Win32 and the microsoft compiler and has
little
to do with the C++ language. Another news group might be more
appropriate..

Thanks for your really fast answer, first of all :)

I was suspecting something about the .lib file, I'll ask to the people
which wrote the .dll if that file is available.

I'll send this message in another newsgroup, if this is not the
appropriate one, sorry for this mistake! (I think you can understand
how much I'm a newbie even from this...^^).

Thanks again

Francesco.
 
S

Sherm Pendley

Thank you very much to you, too! I'll go through the article, it seems
really useful!

regards

Francesco.

Ah, tools like you need help sometimes. That's just the way it is.

sherm--
 
P

Permostat

This person is stalking me. He has created (so far) seven fake profiles
on Facebook using my name and picture, and now he has followed me to
usenet as well.

His ISP is Cox Internet in Metairie, Lousiana. Please report this abuse
to <[email protected]>.

sherm--

Dagnabbit. And I tried to be nice earlier. Well, The real Sherm (not
me) lives in Stupidtown on Dummyface Street. In the great state of Poo
Poo Breath. Please report his smelly butt.

perm--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top