Recent

Author Topic: Converting C header / DLL  (Read 3370 times)

icey

  • New Member
  • *
  • Posts: 19
Converting C header / DLL
« on: June 13, 2013, 03:41:36 pm »
Hi,

i'm trying to embed a c++ dll in my project. The .h file has almost completely been converted by h2pas except for the imported functions: (snippet)

Code: [Select]
typedef int (__stdcall *NxCoreCallbackAPIList) (void* pYourParam,const NxCoreAPIDLLFile* pNcTF);
typedef int (__stdcall *NxCoreListAPIDLLs) (unsigned int controlFlags,NxCoreCallbackAPIList stdcallback,void* pUserParam);

what i've rewritten to

Code: [Select]
type TAPIDLLsListCallback = function (pYourParam: pointer; pNcTF: PNxCoreAPIDLLFile) : cint;

function ListAPIDLLs (controlFlags: cchar; Callback: TAPIDLLsListCallback; pUserParam: cint) : cint; stdcall; external 'api.dll' name 'sNxCoreListAPIDLLs';

The program compiles and starts - so far so good.

if i now call
Code: [Select]

ListAPIDLLs(0, @OnListAPIDLLs, 0);

function OnListAPIDLLs (pYourParam: pointer; pNcTF: PNxCoreAPIDLLFile) : cint;
begin
  ShowMessage(pNcTF^.PathnameStrZ);
  OnListAPIDLLs := 0;
end;

i get exactly one result (should be about 20) and the application crashes   >:(

Does anyone see something i overlooked?




Also i found the following definition:
Code: [Select]
struct NxString {
  unsigned short Atom;  // unique/sequential/nonzero/constant 16 bit number assigned to the String.
  char Text[1]; // array is acually [strlen(Text)+1] and directly follows Atom in memory. };

If i get it right, "Text" can be of whatever length?? Is that even replicable in pascal?

h2pas converted it to
Code: [Select]
    NxString = record
        Atom : cushort;
        Text: array[0..0] of cchar;
      end; 

but i have no clue if that's ok  %)


Greetings
icey

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Converting C header / DLL
« Reply #1 on: June 13, 2013, 04:03:47 pm »
Quote
typedef int (__stdcall *NxCoreCallbackAPIList) (void* pYourParam,const NxCoreAPIDLLFile* pNcTF);
...
type TAPIDLLsListCallback = function (pYourParam: pointer; pNcTF: PNxCoreAPIDLLFile) : cint; // you miss stdcall modifier here
Quote
typedef int (__stdcall *NxCoreListAPIDLLs) (unsigned int controlFlags,NxCoreCallbackAPIList stdcallback,void* pUserParam);
...
function ListAPIDLLs (controlFlags: cchar; Callback: TAPIDLLsListCallback; pUserParam: cint) : cint; stdcall; external 'api.dll' name 'sNxCoreListAPIDLLs';
unsigned int maps to cuint, not cchar.
Quote
Also i found the following definition:
...
If i get it right, "Text" can be of whatever length?? Is that even replicable in pascal
No, it should be exactly one char only. The storage allocated is static.

icey

  • New Member
  • *
  • Posts: 19
Re: Converting C header / DLL
« Reply #2 on: June 13, 2013, 04:41:13 pm »
Hi Leledumbo,

your

Quote
// you miss stdcall modifier here

just made my day, or better my last 2 1/2 days. Stupid me completely overlooked it  :-[ .... If you're ever near Cologne, Germany give me a call, there is a crate of beer waiting for you - or even 2 1/2 ;)


Greetings from a now very happy
icey

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Converting C header / DLL
« Reply #3 on: June 14, 2013, 09:52:24 am »
@icey: hope it's Kölsch brand beer ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018