Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrapping a DLL 1

Status
Not open for further replies.

stackdump

Technical User
Sep 21, 2004
278
GB
Im no expert with C and need some help/advice on how to wrap a Pascal DLL with C.

I have a legacy Pascal application that has no parameters to pass and is basically;

Code:
Library MyLib;
PROCEDURE MyProcedure; export;
BEGIN
   ....
END.
exports MyProcedure;
BEGIN
   ...
END.

Compiling this produces a DLL that I can easily call from within another Pascal program. However, how do I call this DLL within C?

I have tried;

Code:
#include "stdio.h"; 

extern "C" PASCAL MyProcedure; 

int main(int argc, char* argv[]) 

{ 
MyProcedure; 
return 0; 
}

But having tried all manner of variations on this I never get anything that will fully compile since I always get a "Not within target frame" error.

What am I doing wrong?




 
For Microsoft stuff, try
Code:
#include <stdio.h>

extern "C" PASCAL MyProcedure ();

int main(int argc, char* argv[])

{
   // The brackets are important otherwise it just puts
   // the address without doing anything
   MyProcedure ();
   return 0;
}
For other Operating Systems, it may be different; especially the PASCAL keyword.

The most common Pascal problem you will find in C is missing out the parenthesis. Something like
Code:
int checkit ()
{
   ...
}

...

   if (checkit) ...
will compile because checkit is a valid address. It should have read
Code:
   if (checkit ()) ...
 
Thanks for the tips guys, I tried both ways and got the results below;

First of all, CodingNovice's suggestion, I looked these up and think I called them correctly as below;

Code:
#include "stdio.h";

int main() 

{ 

// Check dll exists
if ( !LoadLibrary("MyDLL.dll") )
return 0;

// Check dll contains the procedure
if ( !GetProcAddress( LoadLibrary("MyDLL.dll"), "MyProcedure"  ) )
return 0;

//Run the external procedure
GetProcAddress( LoadLibrary("MyDLL.dll"), "MyProcedure"  );
// Release
FreeLibrary( LoadLibrary("MyDLL.dll") );

return 0; 

}

This compiled but gave me linker errors like these;

Code:
Linker Error (Severity 4)
	Module "a" in file "c:\program files\miracle c\dlltest.obj"
	references unresolved external "_loadlibrary"
	at offset 0008H in segment "_text".


XWB, I tried adding the brackets which made it compile.

Code:
#include "stdio.h";

extern "C" MyProcedure (); 

int main(int argc, char* argv[]) 

{ 
MyProcedure (); 
return 0; 
}

but this also compiled but gave link errors;

Code:
Linker Error (Severity 4)
	Module "a" in file "c:\program files\miracle c\dlltest.obj"
	references unresolved external "_MyProcedure"
	at offset 0004H in segment "_text".

So it looks like Im getting close, but I must be missing out some kind of command about where to link MyProcedure?



 
Well the biggest problem you have at the moment is the fact that your compiler (Miracle C) is basically crap.

As an answer to "write a compiler" for an assignment, it's pretty good.

As a solution for getting real work done - forget it, especially for windows programming.

Ideally, I'd suggest you use Microsoft's visual studio, but if you're on a budget, then I think Dev-C++ makes a pretty good choice.

--
 

ok, I'll load up one of the better compilers as Miracle is turning out to be brown and sticky. (I loaded that one because its free and supposedly good for novices).

But, ignoring the crappy compiler for a moment, is what Im trying to do in the examples above valid?

 
Nope. You are close. After checking that your proc is there you need to capture a pointer to it.
GetProcAddress( LoadLibrary("MyDLL.dll"), "MyProcedure" );
doesn't do what you thought. It returns a pointer to your function if it exists. Theres also no need to call it twice. Just capture the return value in a pointer, cast it to the correct type for your function then call it as you would any other function pointer. For instance if fp is your function pointer then you would call it like this...

fp(params);
or
TYPE retval = fp(params);

Read through the code samples at msdn. Its all there.
 

Success!

I loaded Dev-C (which is much better) and you're right, it is all on MSDN.

Thanks CodingNovice, I learned a lot today, have a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top