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.
 
 
LVL 1
domenicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.