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

Posted on 2008-11-04
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)

Question by:fischermx
    LVL 13

    Expert Comment

    Try a PByteArray instead of PChar. See if you get more.
    LVL 1

    Author Comment

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

    sFile : String;
    pFile : PChar;

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


    How do I use a byte array here?
    LVL 6

    Expert Comment

    In D2009 a PChar is really a PWideChar which is not the same as the C function declaration. You can use PAnsiChar instead.
    LVL 1

    Author Comment

    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.
    LVL 6

    Accepted Solution

    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:

    Open in new window


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now