473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Lab windows dll

HI all,
i have created a dll using Labwindows ..... can anyone suggest me how to use that dll in vc++ ?

with regards
sathya
May 22 '07 #1
2 2539
weaknessforcats
9,208 Expert Mod 8TB
You:

1) call LoadLibrary to load the dll.
2) call GetProcAddress to obtain the address of the function to call. This will be returned as a FARPROC.
3) Use reinterpret_cast<> () to type cast the FARPROC to a function pointer having the correct arguments and return type
4) call the function using the function pointer
5) call FreeLibrary when you are finished with the dll

Note:
A dll is a relic from the C age. That means no functtion overloading and that means no name mangling on the C++ side. The C++ functions must be extern "C" or the dll must have been built using a DEF file containing the export names of the mangled C++ function names.

Example:
Expand|Select|Wrap|Line Numbers
  1.  
  2. int main()
  3. {
  4.     cout << "Calling functions from a dll" << endl;
  5.  
  6.     //First, load the dll into memory
  7.    HMODULE theDll =   LoadLibrary "C:\\Scratch\\ClassDemos\\ADll\\Debug\\ADll.dll");
  8.    if (!theDll)
  9.    {
  10.       cout << "The dll failed to load" << endl;
  11.       return 1;
  12.    }
  13.  
  14.    //Second, get the address of the desried function from the dll
  15.    FARPROC addr = GetProcAddress(theDll, "DisplayFromDll");
  16.    if (!addr)
  17.    {
  18.       //Look up the error in the system errors list
  19.       unsigned int what = GetLastError();
  20.       if (what == ERROR_PROC_NOT_FOUND)
  21.       {
  22.           cout << "Function not found in the dll" << endl;
  23.       }
  24.       else
  25.       {
  26.         cout << "Error: " << what << endl;
  27.       }
  28.      return 2;
  29.    }
  30.     cout << "The function has been located in the dll" << endl;
  31.    //Declare a function pointer that can accept the address of the function.
  32.    //You will need to know the function prototype to do this.
  33.    //Dll function prototypes should be provided by the vendor of the dll
  34.    void (__stdcall *DisplayFromDll)();
  35.    //Type-cast the address returned from GetProcAddress to the function pointer type
  36.    DisplayFromDll = reinterpret_cast<void (__stdcall *)()>  (addr);
  37.    //Now use the function pointer to call the function:
  38.     DisplayFromDll();
  39.  
  40.    //If you don't use a .def file in the dll, you must use the mangled name
  41.    //Second, get the address of the desried function from the dll
  42.    addr = GetProcAddress(theDll, "AreaOfSquare");
  43.    if (!addr)
  44.    {
  45.       //Look up the error in the system errors list
  46.      unsigned int what = GetLastError();
  47.      if (what == ERROR_PROC_NOT_FOUND)
  48.       {
  49.          cout << "Function not found in the dll" << endl;
  50.       }
  51.      else
  52.      {
  53.           cout << "Error: " << what << endl;
  54.      }
  55.      return 2;
  56.    }
  57.  
  58.     cout << "The function has been located in the dll" << endl;
  59.     //Declare a function pointer that can accept the address of the function.
  60.     //You will need to know the function prototype to do this.
  61.     //Dll function prototypes should be provided by the vendor of the dll
  62.     int (__stdcall *AreaOfSquare)(int, int);
  63.     //Type-cast the address returned from GetProcAddress to the function pointer type
  64.    AreaOfSquare = reinterpret_cast<int (__stdcall*)(int,int)>  (addr);
  65.    //Now use the function pointer to call the function:
  66.     cout << "The area of a 6x8 square is " << AreaOfSquare(6,8) << endl;;
  67.  
  68.    //Finally, unload the dll from memory
  69.    FreeLibrary(theDll);
  70.  
  71.    return 0;
  72. }
  73.  
The dll code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void __stdcall DisplayFromDll()
  6. {
  7.     cout << "This function was called from ADLL.dll" << endl;
  8. }
  9.  
  10. int __stdcall AreaOfSquare (int len, int wid)
  11. {
  12.     return len * wid;
  13. }
  14.  
and the dll DEF file:
Expand|Select|Wrap|Line Numbers
  1. ;BEGIN ADLL.DEF FILE
  2. ;This DEF file is required becuase the argument to GetProcAddress()
  3. ;for the function is a C-string and it will never be equal to the
  4. ;C++ mangled name for the function
  5. ;This DEF file maps the mangled name to a name that can be used with GetProcAddress()
  6. ;Note also: Change project settings in Visual Studio to send the LINK this def file.
  7. ;Visual Studio.NET: Project Properties/Linker/Input/Module Definition File/...Path to the def file\Adll.def
  8. LIBRARY ADll 
  9. EXPORTS 
  10. ;Exported Name    C++ Mangled Name
  11. AreaOfSquare   =  ?AreaOfSquare@@YGHHH@Z
  12. DisplayFromDll =  ?DisplayFromDll@@YGXXZ
  13. ;END DEF FILE 
  14.  
May 22 '07 #2
thanks for ur help ...
May 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
7
by: lvpaul | last post by:
Hallo ! I am using IIS-Windows-Authentication in my intranet (web.config <authentication mode="Windows" /> <identity impersonate="true" /> How can I get the users (client) IP-Address ? I...
7
by: Tyler Foreman | last post by:
Hello, I have a strange problem that occurs every so often in my application. It usually takes place when I hide one form and activate another. What happens is I get the following exception:...
1
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. I'm having trouble getting the code that I've written to work, can anyone shed some light as to where I'm...
0
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. The program I'm trying to develop needs to be able to do the following: - Select remote server -...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
2
by: sambo251 | last post by:
After running a few updates I get this very annoying "Windows Installer" error #1706 that will ne go away! It keeps saying that it cannot find the file "instantsharedevices.msi", that it is on...
1
by: mfunkmann | last post by:
Hi, I recently got an error and I don't know how to fix it: Error 1 'System.Data.DataColumn' does not contain a definition for 'Windows' C:\c#\CsharpPRO\Form1.Designer.cs 304 77 CsharpPRO I...
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: List of autostart locations Linked from the Original article- "Windows Autorun FAQs: Description". Que: Can you list all the autostart locations for windows? Ans: Here is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.