?
Solved

Calling a function from Delphi 2009 to a C++ Dll

Posted on 2008-11-04
5
Medium Priority
?
803 Views
Last Modified: 2008-11-22
So, I have to use a previously compiled DLL made in C++.

There's a function defined as :
extern "C" SHORT DLLExport WINAPI OpenThisFile(PBYTE cpFileName)

I used to have the next code in Delphi 5,6,7,2005,006 and 2007 which used to work:
OpenThisFile: function(cpFileName: PChar); SmallInt stdcall

But now, in Delphi 2009 that seems broken, it seems from the error messages I'm getting that the C++ function just reads the first character of the filename I'm passing.

How do I do that now in Delphi 2009? (and preferable be backward compatible with Delphi 2007 and older)

0
Comment
Question by:fischermx
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:ThievingSix
ID: 22883546
Try a PByteArray instead of PChar. See if you get more.
0
 
LVL 1

Author Comment

by:fischermx
ID: 22887264
I know this is a Delphi question, but how do I copy my data from the string to the bytearray?
I have currently have:

var
sFile : String;
pFile : PChar;
begin

sFile := 'test.txt';
pFile := @sFile[1];

OpenThisFile(pFile);

How do I use a byte array here?
0
 
LVL 6

Expert Comment

by:ChristianWimmer
ID: 22887347
In D2009 a PChar is really a PWideChar which is not the same as the C function declaration. You can use PAnsiChar instead.
0
 
LVL 1

Author Comment

by:fischermx
ID: 22887392
I already tried PAnsiChar and it behave exactly the same. I changed the parameter and the declaration in Delphi from PChar to PAnsiChar, and the C++ function still see only the first character.
0
 
LVL 6

Accepted Solution

by:
ChristianWimmer earned 2000 total points
ID: 22887497
Usually if an ANSI char function only reads the first char, the problem is a NULL character at the second place right behind the first char.
Maybe you're using type String in D2009 through your app which is a UnicodeString.
So try to convert the String to ANSI at first:
OpenThisFile(PAnsiChar(AnsiString(MyString)));

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question