473,597 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DLL's, Borland & alot of Frustrations

Airslash
221 New Member
Hi,

I'm sorry if this is not the correct forum, but since I'm programming in C++ this looked like the best place.

I'm trying to make a nice DLL file for the company where we place all functions, classes and definitions in it to share amongst our various software projects.
These things are written in C++.

At the moment I havent done anything special yet to the files suchas including those magic __ words for the export etc.

We are using CodeGears Borland C++ builder as main development IDE.
Our first test was to put our GDI+ wrapper in the DLL and call it from one of our projects.

But we have some issues getting the DLL linked.
Can anyone provide me with a explanation as to what needs to be done in order to make the C++ classes inside the DLL exportable and to link the DLL to our projects using Borland CodeGears?



this IDE is driving me nuts.....in VS its just a few clicks to get it sorted....
Dec 8 '09 #1
11 4702
weaknessforcats
9,208 Recognized Expert Moderator Expert
You are using Windows, yes?

First decide on where you are going to use explicit or implicit linking.

Explicit linking means you will call LoadLibrary to loas the DLL and then call GetProcAddress for the address of your function in the DLL and then you will call the function at that address.

Implicit linking means you will call the DLL function as you would any other function. Here you use the .h file created by the DLL build. The function you call in that .h does the LoadLibrary, the GetProcAddress and the call to the function at that address. This implicit linking requires a _dllimport in the .h file function prototype. If this is not there, then you have to use explicit linking.

Maybe you could explain further about your linking issue.
Dec 8 '09 #2
Airslash
221 New Member
Yes,
We're developing for Windows Server 2003 at the moment.

The basic idea is to have one DLL where all the classes, structs and functions are that are commonly shared between the various small applications that we develop.
An example I recently developed would be a GDI+ wrapper.

At the moment I have created the header and class file for this wrapped and placed them inside the DLL project. Nothing else in it at the moment.

The idea now is that when I create a new application, I can simply type for example:

Expand|Select|Wrap|Line Numbers
  1. Wrapper x = new Namespace::Wrapper();
  2.  
Without having to worry about all the required files for this class, since they're all in the DLL and beeing linked for the Project.
What I would like to know, do I need to add anything speciall to the class definitions inside the DLL or in the Project.

We've been trying several things with the #define and #pragma statements in a project, and the project compiled, but as soon as we called one of the classes/functions from the DLL the application refused to run or even start up at some point.
Dec 8 '09 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
First, all the code in the DLL can be in C++ except the functions you call from your program.

A DLL is an old C whiz-bang where you use the name of the DLL funcrtion in your program:

Expand|Select|Wrap|Line Numbers
  1. FARPROC ptr = GetProcAddress(hModule, "AreaOfSquare");
Unfortunately, AreaOfSquare is never found in the DLL. The C++ compiler has mangled the name in the DLL. Therfore, the function in the DLL must be extern "C":

Expand|Select|Wrap|Line Numbers
  1. //In the DLL:
  2. extern "C"
  3. int __stdcall AreaOfSquare(int len, int wid)
  4. {
  5.      ACplusplusFunction();
  6. }
The ACplusplus function does not need to be extern "C" since it is never called using a literal "ACplusplus ".

So, your DLL interface must be designed correctly.

The extern "C" can be avoided by using a DEF file to export the mangled name as an alias:

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

Lastly, the DLL export functions must use the __stdcall convention (WINAPI).

If you use GetProcAddress and you are successful in getting a non-null FARPROC returned, I would typecast that pointer into a function pointer of the correct type. That way when I call the DLL function inside my program, I can just use the name that was coded inthe DLL in the first place:

Expand|Select|Wrap|Line Numbers
  1. FARPROC ptr = GetProcAddress(hModule, "AreaOfSquare");
  2. int (__stdcall *AreaOfSquare)(int, int);
  3.     //Type-cast the address returned from GetProcAddress to the function pointer type
  4.     AreaOfSquare = reinterpret_cast<int (__stdcall*)(int,int)>  (addr);
  5. cout << "The area of a 6x8 square is " << AreaOfSquare(6,8) << endl;
  6.  
Dec 8 '09 #4
Airslash
221 New Member
woah,

complex :)


But if I understand it correctly, I need to make some kind of C function that for example fetches my C++ class for me, and that should work without a problem then ?
Dec 8 '09 #5
RRick
463 Recognized Expert Contributor
I have worked a little bit with DLLs and found that they have issues with what can cross between the DLL library to your application.

For example, if something is newed in the DLL, then it needs to be deleted in the DLL. STL containers in the DLL have issues when accessed/modified by your app.

I'm not sure of the specifics and this was a while ago. Perhaps someone else can clarify these issues.
Dec 8 '09 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
It's not that complex.

I would get a copy of Windows via C/C++ by Jeffrey Richter where all this is documented.

But yes, you write a function in the DLL that is called by the program. That function can create C++ objects, call member functions, etc. But the name of this function cannot be mangled. That's because you are asking for the funciton by name (a literal) in the GetProcAddress call. If the compiler has mangled the name in the DLL, then the mangled name will never match the literal in the GetProcAddress call. Therefore the function must be extern"C" in the DLL code.

Please note: Bacause the DLL is compiled in C++ and the interfacing functions are called like C functions, you are quite OK to use this C++ DLL in a hard C program. Heck if that C program interfaces FORTRAN, you can access C++ code from FORTRAN programs.

I have attached a ZIP for a DLL example that works in Visual Studio.NET 2008. Compile using a DLL project.
Attached Files
File Type: zip DLL.zip (1.6 KB, 177 views)
Dec 8 '09 #7
Airslash
221 New Member
Alright,

I'll definitly give it a look when I'm back at work (23.00 while writing this).
Been looking around a bit more on the Internet regarding the whole stuff of DLLs and doubting a bit whether or not I should make a static .lib instead of the dynamic .dll file.

Most things are still a big mystery to me on this subject since I'm only a junior C++ not so long graduated, but have to bite the bullet sometime.

Definitly big thanks for the replies.
Still got a load of questions and probably will run into more problems, but at least I have a better understanding of the whole subject.
Dec 8 '09 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
A static lib will make your program larger and removes the ability to change the program by just chaing the DLL. Often it's easier to keep the main installed base and just produce new DLL versions for your upgrades.

Also, your program could read a .ini file and in there could be the name of the DLL to use. This allows your program to chnage the name of the DLL that's loaded without changing the code.

Try, always, to have no hard-coded assumptions in that main program.

A Visual Studio. NET DLL project produces both a .dll and .lib. The difference is that if your use the .lib, the function there calls LoadLibrary, GetProcAddress and the function in the DLL. This is the implicit linking. You will notice a __dllimport in the .h file.
Dec 9 '09 #9
Airslash
221 New Member
Well,

we solved the problem finnally.
Call me a nunchkimpoop but the problem was that the application couldn't find the DLL and the Borland Debugger wasn't smart enough to tell us so.

We found out by running the compiled exe files outside the IDE and then got the missing DLL error.
Now everything is working as it should and the DLL is working like a charm.

Even have classes in it without C function calls, just plain C++ stuff that can be called without problems. Was a bit tricky to get the __dllexport and __dllimport stuff in the right places, but all works.

a new thing learned :)
Dec 9 '09 #10

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

Similar topics

0
2905
by: Mike Ruskai | last post by:
I've been trying to implement database axis using the C api for a many-threaded application (several, actually), and keep running into a problem on server disconnect. One of the main reasons is to get around the packet size limit of ODBC (which seems to be somewhere around 10MB), which prevents a few large BLOBs from being inserted into the database (and retrieved, of course). While debugging, the call to mysql_close() simply hangs. If...
1
2135
by: AP | last post by:
Hi, Is there anyway to call a DLL developed in c# from a borland c++ 5.0 application? Is there anyway to do this without having to have the .NET framework installed (compile C# to native library?). Thanks, Adam
1
1635
by: Furer Ramon | last post by:
I need to call the function mul3 in a dll: extern "C" unsigned __declspec(dllexport) mul3(unsigned npts, const double * src, double * dst); from vb.net how can i do this? Greets
1
11975
by: Terry | last post by:
Is there any hope of using Borland Developer Studio 2006 to link in a DLL compiled by MS Visual C++ version 6? I have a Win32 DLL from a company named Vocera. The DLL defines C++ classes that I want to instantiate and use. It is not COM-compliant. It came with .dll, ..lib, and .h files. I tried using Borland C++Builder 2006, but when I add the .lib file to my project, I get the error: " Error: 'C:\VMI.LIB' contains invalid OMF...
1
3609
by: Michael Buechel | last post by:
Hi NG, I've copied this from the article Q309801 (http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B309801). ---- schnipp --------- You may receive an LNK2019 error message when you build a Visual C++ 2005 or Visual C++ .NET application that uses a CString-derived class from a DLL file ---- schnapp ---------
3
3328
by: deepha | last post by:
hai , I have a app which is using a xxxx.lib file. I am compiling this application on borland c++ compiler using make file. i am having the xxxx.dll also. I just want compile the application using the dll. If any one knows how to use the dll please suggest... thanks, deepha
17
7214
by: Fabry | last post by:
Hi All, I'm new of this group and I do not know if this is the correct group for my question. I have a DLL with its export library (.lib) wrote in Borland C++ 6. In borland everything is OK and I am able to include the lib and use the class that i have exported creating an instance with new etc... I would like to export this class in microsoft VC++ using the same .lib file. Obviously it doesn' t work.
1
4700
by: =?Utf-8?B?SmNr?= | last post by:
I am trying to access the registry from a normal user by the following code .... but failed!! However, it works under admin user logon. I think I need to grant some access right to the normal user, but I don't know how! Can anyone give me a hand please? Many Thanks!! ^_^ ' ---- Code start Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOp ...
7
10213
by: SteelNetNob | last post by:
I have created a COM callable DLL in C# .NET 2.0 and created a TLB from the assembly using the .NET regasm tool. In Borland C++ Builder 4.0 I go to Project->Import Type Library-> and find my DLL's type library there and click "Ok" to import it. BCB creates an HardwareCheck_TLB.cpp & HardwareCheck_TLB.h file.
0
7967
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7885
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6687
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5428
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2403
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.