Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to add DLL files to MS VC++ and use them?

Status
Not open for further replies.

elrayes

Full Member level 5
Full Member level 5
Joined
Jun 3, 2008
Messages
244
Helped
20
Reputation
40
Reaction score
7
Trophy points
1,298
Location
Egypt/Canada
Activity points
2,777
pcan_usb.dll

Hi

I want to know how to add ready made DLL files to MS visual studio and use them in VC++, I tried to search google but find nothing could help, I know its easy but for me not cause i'm not an experienced programmer.

Thanks
 

add dll vc++

try here, not sure if this if for the right version of MSVC...

http://msdn.microsoft.com/en-us/library/3y1sfaz2(VS.80).aspx

Haven't done this in C++ myself as I tend to cheat and use C# for gui stuff and using dll's is far easier.

Best of luck :)
 
call dll vc++

Hi elrayes,

Could be done with LoadLibrary(), GetProcAddress(). If function pointer is valid (!= NULL) you simply call dll function. lpProcName parameter is name of function stored in dll. consult MSDN.

Best regards,
Alex
 
how to add dll to vc++ project

The term ready made DLL files isn't completely clear. To my opinion, it would involve a *.lib and *.h or *.hpp file supplied with the dll (and a documentation and example application, too) . Then interfacing the dll is easy. Otherwise, you have to generate an import library and possibly must guess the function parameters and calling type. In case of a c++ dll, recognizable by the MS typical decorated names, it's easier, cause the tools can extract the function parameters from the dll.
 
how to add dll to c++ project

Actually i did add the *.h file to the include file and *.lib file to the libraries file and used some of these functions and it worked, but still don't know how to use the DLL, where i should place the *.dll file itself, with my project or in include or where?, beside can any one write down the syntax of importing a DLL to a program.
Thanks for your replies.
 
vc++ dll include

There are two ways of using a dll, static linking or dynamic linking, with static linking, you just include the 'h' file and add the 'lib' file to the project.
With dynamic linking, the dll must be in the search path of the program and it is loaded at runtime. Put it in the same directory as the exe.
I dont use Visual studio myself, too complicated and bloated in my opinion, I use Borland Builder. But the functions and methods are more or less the same.
Here is the code I use to load the functions from 'pcan_usb.dll' which supports a usb interface to a can dongle. It should give you some ideas. You should find most of the dll calls and types are the same or very similar.

Code:
//---------------------------------------------------------------------------

#include "PCAN_USB.H"

//---------------------------------------------------------------------------

HINSTANCE g_i_DLL;
char g_LibFileName[] = "PCAN_USB.DLL";

typedef DWORD (__stdcall *PCAN_Init)(WORD wBTR0BTR1, int CANMsgType);
typedef DWORD (__stdcall *PCAN_Close)();
typedef DWORD (__stdcall *PCAN_Status)();
typedef DWORD (__stdcall *PCAN_Write)(TPCANMsg* pMsgBuff);
typedef DWORD (__stdcall *PCAN_Read)(TPCANMsg* pMsgBuff);
typedef DWORD (__stdcall *PCAN_VersionInfo)(LPSTR lpversioninfo);

PCAN_Init g_CAN_Init;
PCAN_Close g_CAN_Close;
PCAN_Status g_CAN_Status;
PCAN_Write  g_CAN_Write;
PCAN_Read g_CAN_Read;
PCAN_VersionInfo g_CAN_VersionInfo;

//---------------------------------------------------------------------------

bool __fastcall InitCan(void)
{
if(Load_dll() == false)
  {
  Application->MessageBox
  ("The required file 'PCAN_USB.DLL' can not be loaded?", "error", MB_ICONSTOP);
  return false;
  }

return true;
}

//---------------------------------------------------------------------------

bool Load_dll(void)
{
if(g_i_DLL == NULL)
  {
  g_i_DLL = LoadLibrary(g_LibFileName);

  if(g_i_DLL == NULL)
    return false;

  if(GetFunctionAdress(g_i_DLL) == false)
    return false;
  }
return true;
}

//---------------------------------------------------------------------------

bool GetFunctionAdress(HINSTANCE h_module)
{
if(h_module == NULL)
  return false;

g_CAN_Init = (PCAN_Init) GetProcAddress(h_module, "CAN_Init");

if(g_CAN_Init == NULL)
  return false;

g_CAN_Close = (PCAN_Close) GetProcAddress(h_module, "CAN_Close");

if(g_CAN_Close == NULL)
  return false;

g_CAN_Status = (PCAN_Status) GetProcAddress(h_module, "CAN_Status");

if(g_CAN_Status == NULL)
  return false;

g_CAN_Write = (PCAN_Write) GetProcAddress(h_module, "CAN_Write");

if(g_CAN_Write == NULL)
  return false;

g_CAN_Read = (PCAN_Read) GetProcAddress(h_module, "CAN_Read");
if(g_CAN_Read == NULL)
  return false;

g_CAN_VersionInfo = (PCAN_VersionInfo) GetProcAddress(h_module, "CAN_VersionInfo");
if(g_CAN_VersionInfo == NULL)
  return false;

return true;
}

//---------------------------------------------------------------------------

void __fastcall Close_can(TObject *Sender)
{
if(g_i_DLL)
  {
  FreeLibrary(g_i_DLL);
  g_i_DLL = NULL;
  g_CAN_Init = NULL;
  g_CAN_Close = NULL;
  g_CAN_Status = NULL;
  g_CAN_Write = NULL;
  g_CAN_Read = NULL;
  g_CAN_VersionInfo = NULL;
  }
}

//---------------------------------------------------------------------------

Hope this helps.
 
dlls in vc++

Also with static linking, the *.dll file has to be in the actual path. It's most simple to place in in the *.exe directory, installers often put it into system32.
 
how to add a dll to vc++

btbass,
Thanks for your code sample (I use Borland Builder too). I'm using the pcan_usb.dll for the first time in one of my projects.

I've come across a problem I hope you can give me some advice on. Some of my users like to open multiple sessions of my application. I realize that it's not possible for two programs to communicate to the PeakCan dongle simultaneously, but these users are not using the PeakCan, they are using the program for other things.

Unfortunately when using the pcan_usb.dll, with static linking, the second instance of my application will not launch. I get the error "The application failed to initialize properly 0xC0000142." I use other dlls in this project, statically linked, and they don't give me a problem.

So I saw your example, and decided to try dynamically linking the dll file using your code. This time I'm able to launch 2 copies of my application, but when I call InitCan() in the second instance, I get the error message "The required file 'PCAN_USB.DLL can not be loaded?" (This will work for me, the error will not occurr until the user actually tries to access the PeakCan dongle in the second instance).

But I'm curious, do you know if there is something in the PCAN_USB.DLL the prevents it from be loaded if another program has already loaded it? I'm guessing it's by design so that two programs don't try to access the PeakCan dongle simultaneously. I've just never seen this behavior before in a dll file and was hoping you could sheld some light on it.

Thanks for you help.
 
vc++6 how to add dll path

I can not be sure, but could it be something to do with the usb driver?
The program will connect to the dongle using the usb port, maybe when the other program tries to access the same usb device the error occurs, as it is already being controlled by the first instance.
Have you tried two dongles? Then the usb enumeration might allocate one dongle for each program.

I am working with Visual Studio and C# now. Using .net assemblies is so much easier, and C# is a pretty cool language.
 

adding dll file

btbass,
Thanks for your reply. I did some more testing and it actually fails in the call to LoadLibrary ("PCAN_USB.DLL"). I was guessing that it failed in the g_CAN_Init() function. At any rate, it's apparent that the PCAN_USB.DLL can only be loaded into memory once when only one USB dongle is on the system. I only have one USB dongle so I'm unable to test the case with more than one USB dongle.

By the way, I rewrote my program to use dynamic linking and the user is now able to launch multiple instances of the program now. So I'm good to go.

I've done some work with VB.Net, but I haven't ventured into the land of C# yet.
 

how to add library into vc++ 2008

try them by putting them in windows folder named as either Windows\system32
 

Hello Guys,

I know its a very old thread but I thought about sharing my experience might be useful for someone one day.
First thanks for all your replies.

To use the DLL file with any windows application you have two options:

- copy the .dll file(s) to the project directory.

or

- copy the .dll file(s) to the executive files directory.

both ways worked with me using Visual Studio.

Thanks,
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top