• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Need help with using MS VC++ dll with Delphi4

I have created a MS Visual C++ dll which my teamworker wants to
incorporate in his Delphi4 application. The GetProcAddress() fails when it attempts to retreive one of the functions associated with the dll. I would like to know what the basics are with importing MS dlls, and are there any issues that I must deal with from the dll side. What are the limitations, restrictions surrounding such practice? I have browsed several Delphi-related sites but I have not found any that deal
specifically with importing the Microsoft VC++ dlls into Delphi4. Any and all help is greatly appreciated. Thanks.

Platform MS NT 4.0
 
hMidasInst := LoadLibrary(‘Midas.dll’);
if ( hMidasInst <= 0 ) then
raise exception.create(‘[LoadLibrary Fail]’);
try
try
Initialize := GetProcAddress(hMidasInst, PChar(‘Midas_Initialize’)); if not assigned(Initialize) then
raise exception.create(‘[GetProcAddress Fail]’);
Initialize(pPtr1, pPtr2, hMidasInst);
sStr := pPtr1 + ‘ test ‘ + pPtr2;
ShowMessage(sStr);
except
end;
finally
FreeLibrary(hMidasInst); end; end;
end.
 
 
0
domenic
Asked:
domenic
  • 4
  • 3
  • 3
  • +2
1 Solution
 
simonetCommented:
Do it like this:

Type
  TInitializeFunction = function(parameters : paremetertype) : resulttype; stdcall;

var
 Initialize : TInitializeFunction;

Now, in your code:


@Initialize := GetProcAddress(hMidasInst, PChar(‘Midas_Initialize’));
{ Note that I am using  "@Initialize", not "Initialize" }


if not @Initialize=0 then
 { issue error message }

Yours,

Alex


0
 
simonetCommented:
Ops, I made a terrible mistake there. The "if" clause must look like:

if @Initialize=0 then { issue error message }

Alex
0
 
rwilson032697Commented:
One thing to note is that the procedure names are case sensitive. You can use QuickView to see the names of the exported DLL procedures to check...

Cheers,

Raymond.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
simonetCommented:
>You can use QuickView to see the names of the exported DLL

Right! Or use Resource Explorer (much better than QuickView or TDump):

http://www.bhnet.com.br/~simonet/resexp.htm

Alex
0
 
philipleighsCommented:
The dll may have munged function names.

eg. ??0Midas_Initialise@@QAE@XZ

in which case you have to alias the function name. (QuickView will show munged names in this case).

Cheers,
Phil.
0
 
MadshiCommented:
Alex, just one little hint (please don't be annoyed... :-)

You can leave the "@" away in this line:

  Initialize := GetProcAddress(hMidasInst, 'Midas_Initialize');

And a hint to domenic: You don't need to convert string constants to pchar. Instead of "GetProcAddress(dll, pchar('funcion'))" simply use "GetProcAddress(dll, 'function')".

:-)

Regards, Madshi.
0
 
domenicAuthor Commented:
Thanks for all the hints but I think Phil hit the nail on the head. Apparently, the function names are part of a 'munged' name as shown with QuickView. Once these names were used, success followed. One last comment, why are the function names affected this way and is there a way to avoid this?

Thank you all.
0
 
MadshiCommented:
Hehe, yes, write your dlls in Delphi...  :-))
0
 
rwilson032697Commented:
If you declare the functions in the VC++ DLL as extern "C", then the names should not be munged...

Cheers,

Raymond.
0
 
domenicAuthor Commented:
GetProcAddress clearly failed because of the name mismatch. I used the extern "C" to correct the manled function names as shown with QuickView.
0
 
rwilson032697Commented:
Well done Phil! :-)

Cheers,

Raymond.
0
 
philipleighsCommented:
keh? Do I know you hippy?  ;-)
0
 
domenicAuthor Commented:
Raymond, thank you for your quick insight on using the extern "C" to clear the mangled function names. I would have moved some points over your way but I don't think it is set up to accommodate more than one answer.
0
 
rwilson032697Commented:
No, EE isn't. What usually happens is the original Q is deleted and dummy ones substitued to distrbute the points...

Cheers,

Raymond.

PS Hippy yerself Phil!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now