473,386 Members | 1,743 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,386 software developers and data experts.

use Visual C++ dll in c++ builder

Hi all, I have a similar problem:

I have a dll compiled with Visual C++ but I don´t have the .lib or .def associated files. This dll has C style functions and methods inside classes, there is no problem when I call a C style function, but I can´t call the methods of the different classes implemented on the dll library (The linker says: unresolved external...).

I think the problem is something with the names used, because when I generate de .def file, the C style functions names are of this type:

pcre_get_substring_list @2841; pcre_get_substring_list
pcre_malloc @2842; pcre_malloc
pcre_stack_free @2843; pcre_stack_free
pcre_stack_malloc @2844; pcre_stack_malloc
pcre_study @2845; pcre_study
yychar @2846; yychar
yyin @2847; yyin
yyleng @2848; yyleng

But the names of the methods implemented in the different classes are really strange:

_Z12PrintDepTreeN4treeI7depnodeE17preorder_iterato rEi @1 ; _Z12PrintDepTreeN4treeI7depnodeE17preorder_iterato rEi
_Z13yy_scan_bytesPKci @2 ; _Z13yy_scan_bytesPKci
_Z14yy_init_bufferP15yy_buffer_stateP6_iobuf @3 ; _Z14yy_init_bufferP15yy_buffer_stateP6_iobuf
_Z14yy_scan_bufferPcj @4 ; _Z14yy_scan_bufferPcj
_Z14yy_scan_stringPKc @5 ; _Z14yy_scan_stringPKc
_Z15yy_flush_bufferP15yy_buffer_state @6 ; _Z15yy_flush_bufferP15yy_buffer_state
_Z16yy_create_bufferP6_iobufi @7 ; _Z16yy_create_bufferP6_iobufi
_Z16yy_delete_bufferP15yy_buffer_state @8 ; _Z16yy_delete_bufferP15yy_buffer_state
_Z19yy_switch_to_bufferP15yy_buffer_state @9 ; _Z19yy_switch_to_bufferP15yy_buffer_state
_Z20yy_load_buffer_statev @10 ; _Z20yy_load_buffer_statev

Is it possible to call a method of a class? what should I do?

Thank you very much.
Nov 2 '07 #1
5 6531
weaknessforcats
9,208 Expert Mod 8TB
What happens is that a DLL is an old C device. In C function names are unique so you can call the function by using the function name.

However, to support function overloading in C++, the compiler hashes (camoonly called mangling) the function anme plus the arguments into a unique name. It this unique, or mangled, name that is actually called in the compiled code.

Now you can't call the C++ function by name.

What you have to do is add and exports section to your .def file an export the mangled name as something else. Of course, you lose function overloading but you never had that in C anyway.

.def formats vary but it will be something like:
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. AreaOfSquare1 =  ?AreaOfSquare@@YAXH2W3
  13. ;END DEF FILE 
  14.  
Nov 2 '07 #2
Thank you very much for your help.

I tried what you told me and I have errors. I have a file called 'morfo.dll', with the 'impdef' tool of C++ Builder I get the 'morfo.def' file, which has teh format I told you:

LIBRARY MORFO.DLL

EXPORTS

_ZN9tokenizer8tokenizeERKSs @1105; _ZN9tokenizer8tokenizeERKSs
_ZN9tokenizer8tokenizeERKSsRm @1106; _ZN9tokenizer8tokenizeERKSsRm
_ZN9tokenizerC1ERKSs @1107; _ZN9tokenizerC1ERKSs
_ZN9tokenizerC2ERKSs @1108; _ZN9tokenizerC2ERKSs

...

(This functions correspond to a class 'tokenizer' with 2 constructors and 2 functions named 'tokenize'). As you told me, I tried modifiying morfo.def:

LIBRARY MORFO.DLL

EXPORTS

tokenize = _ZN9tokenizer8tokenizeERKSs @1105; _ZN9tokenizer8tokenizeERKSs
tokenize1 = _ZN9tokenizer8tokenizeERKSsRm @1106; _ZN9tokenizer8tokenizeERKSsRm
tokenizer = _ZN9tokenizerC1ERKSs @1107; _ZN9tokenizerC1ERKSs
tokenizer1 = _ZN9tokenizerC2ERKSs @1108; _ZN9tokenizerC2ERKSs

...

I included 'morfo.def' in my project but I had two problems:

[Linker warning]: Attemp to export non-public symbol '_ZN9tokenizer8tokenizeERKSs'

If I get 'morfo.def' automatically from morfo.dll I suppose that morfo.dll really export this symbols but... It could be something about the format??

And:

[Linker error]: Unresolved external: tokenizer::tokenizer(const STL::basic_string)

So the problem persists...

Another thing I tried it's getting a static library 'morfo.lib' with 'implib' tool of C++ Builder, and I tried adding it to my project, but the problem continues...

What should I do?

Thank you very much!
Nov 5 '07 #3
Just one more thing, I tried too with 'morfo.def' modified and added to my project and dynamic loading. To call the constructor I define:

typedef tokenizer * __stdcall (* FPTR)(const string &);

and I use this code:

tokenizer *tok;

HMODULE dllHandle = LoadLibrary("morfo.dll");
FPTR cons_tok = (FPTR) GetProcAddress(dllHandle, "tokenizer1");

tok = cons_tok("hola");

(tokenizer1 is the name I assigned to the constructor that recives a string, see it in my earlier post) But I have the same linker Warning I told you before and a run-time error, How can I call the constructor?

Thank you!!
Nov 5 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
(tokenizer1 is the name I assigned to the constructor that recives a string, see it in my earlier post) But I have the same linker Warning I told you before and a run-time error, How can I call the constructor?
Remember, this is C.

There are no constructors, methods, etc. Member functions of classes require that the address of the object be passed as the first argument. The compiler does this secretly so the arguments in the class do not have one for a pointer to a class object. If you try to put one in, things don't compile.

Bottom line: You cannot call member functions (that incudes constructors) from C.

You can only call C functions.

So to create an object, you write a C function in the dll that you can call and have that function create the object:
Expand|Select|Wrap|Line Numbers
  1. void* Create(char* str)
  2. {
  3.      return (void*)new MyClass(str);
  4. }
  5.  
Then in C you can:
Expand|Select|Wrap|Line Numbers
  1. void* ptr;
  2. void* (*Create)(char*);   //the Create function pointer
  3. HMODULE dllHandle = LoadLibrary("morfo.dll"); 
  4. Create = void*(*)(char*) GetProcAddress(dllHandle, "Create");
  5. ptr  = Create("hola");
  6.  
Now ptr has the address of a MyClass object.

To call a member function MyClass::MethodA(void* obj,int arg), you write another C function in the dll:

Expand|Select|Wrap|Line Numbers
  1. void* MyClassMethodA(void* obj, int arg)
  2. {
  3.      MyClass* ptr = (MyClass*)obj;
  4.      obj->MethodA(arg)
  5.      return (void*) obj;
  6.  
  7. }
  8.  
Then back in C:
Expand|Select|Wrap|Line Numbers
  1. void* (*MyClassMethodA)(void*, int);  //the function pointer
  2. MyClassMethodA = void*(*)(void*, int) GetProcAddress(dllHandle, "MyClassMethodA");
  3. ptr  = MyClassMethodA(ptr, 25);
  4.  
Functions like MyClassMethodA are called wrappers and you will need to write one of these inthe dll for each of your class member functions that you want to call from C.
Nov 5 '07 #5
Thank you so much for your help!!!!!
It´s been really useful!!
Nov 6 '07 #6

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

Similar topics

0
by: Christian Mendieta | last post by:
Hi In VB6 there was a class builder... is there any tool in Visual Basic .NET that can do the same? Generate classes Thanks Christian Mendieta
0
by: Jaume | last post by:
J4L Components (http://www.java4less.com/reports_dotnet.html), supplier of components for web developers announces the availability RReport Visual Builder for .NET. This is the c# version of the...
17
by: Ziggi | last post by:
Hi. I want to get a C++ IDE, but I dont know whether to go for Bill Gate's solution or Borland's. Could any kind folks detail the relative strength and weaknesses of both, and also tell me which...
15
by: Chris | last post by:
I am just beginning programming again and need a bit of advice. I have both Visual C++ 6.0 Standard Edition and Borland C++ Builder 6. Of these two which do you consider the best for programming...
1
by: Bob | last post by:
Trying to use VBisual web developer express 2005 beta 2. Dragged an accessdatasource to the blank web page. Started to configure. Entered location of an access database. On next page, clicked...
0
by: sales | last post by:
I am glad to present to the community of this group the new version of our component intended for visual building of SQL queries via an intuitive interface - Active Query Builder...
1
by: Farshid | last post by:
Hi Is there any way to use Visual Studio Query Builder in Windows Forms applications. I think query builder dialog is in Micosoft.VSDesigner assembly but i dont know how to use it. Thank You...
8
by: king kikapu | last post by:
Hi to all, i am not sure if this question really belongs here but anyway, here it goes: I have seen a lot of IDEs for Python, a lot of good stuff but actually none of them has what, for example,...
28
by: grappletech | last post by:
I took Pascal and BASIC in a couple of beginner programming courses about a decade ago and did well with them. I am good with pseudocode, algorithms, and the mathematics of programming. I decided...
6
by: FrozenDude | last post by:
Does anyone know of or have a favourite 3rd party Visual Query Builder? We are looking for a very easy to use interface that provides SQL code verification, table joins etc. Thanks, Dave
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.