Reputation: 55
i have a c function 'cGetLen' that i want to call it from delphi2009 but got the above titel Error.
any help please ?
here is the code
unit testpas;
interface
function dFunc():Integer ; stdcall;
implementation
function cGetLen(str1, str2: PAnsiChar): Integer ; cdecl; external;
function dFunc():Integer ; stdcall;
var
fstr1: ANSIString;
sstr2: ANSIString;
begin
fstr1 := 'hello';
sstr2 := 'there';
dFunc := cGetLen( PAnsiChar(fstr1), PAnsiChar(sstr2) );
end;
begin
end.
Upvotes: 0
Views: 1938
Reputation: 612944
The first problem is that you are not linking an external object. You would need to include:
{$LINK cGetLen.obj} // or whatever the object file is called
somewhere in the unit.
Once you've done that you will probably face another problem because the C compiler will decorate the name of the function. Typically, for a cdecl
function, by prefixing an underscore. So you would import the function like this:
function cGetLen(str1, str2: PAnsiChar): Integer ; cdecl; external name '_cGetLen';
or
function _cGetLen(str1, str2: PAnsiChar): Integer ; cdecl;
The other common problem you will face is that the C function calls other functions that cannot be resolved. They would need to be either implemented in separate object files that you linked, or implemented in your Pascal code.
Do bear in mind that I cannot see your C code, nor do I know how you compiled it. So, these problems may not afflict you, or indeed you may be facing other problems. An interop question like this really does demand full disclosure.
To be perfectly honest, you appear to be quite a long way from success here. I suggest you pause this task, and get a firm grasp of this subject matter. Start here: http://praxis-velthuis.de/rdc/articles/articles-cobjs.html
Upvotes: 4