Link to home
Start Free TrialLog in
Avatar of domenic
domenic

asked on

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.
 
 
Avatar of simonet
simonet
Flag of Brazil image

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


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

if @Initialize=0 then { issue error message }

Alex
Avatar of rwilson032697
rwilson032697

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.
>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
ASKER CERTIFIED SOLUTION
Avatar of philipleighs
philipleighs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of domenic

ASKER

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.
Hehe, yes, write your dlls in Delphi...  :-))
If you declare the functions in the VC++ DLL as extern "C", then the names should not be munged...

Cheers,

Raymond.
Avatar of domenic

ASKER

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

Cheers,

Raymond.
keh? Do I know you hippy?  ;-)
Avatar of domenic

ASKER

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.
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!