windowspointerspascal

Why does this pointer throw Access violation?


I try to translate a pointer in my code.But it throw Access violation.

I tried many ways on the Internet,but they were all failed.

the main program:

createthread(nil,0,@make,@cli,0,tmp);//cli is a longint

and the procedure of make:

procedure make(a:pointer);
var
 c:longint;
begin
 c:=PLongint(a)^;
 //here throw Access violation

 ...
end;

I expect to get the number in pointer a,how can I do this?


Solution

  • There are several things you need to do to successfully call a C function (like the Win32 CreateThread() API) from Pascal.

    One of them is set the correct calling sequence: stdcall.

    Other is get the pointers correct (your original question).

    Yet another issue is to make sure the C function declaration is capitalized correctly (e.g. CreateThread, not createthread.

    You haven't shown us your Pascal declaration, nor told us what Pascal compiler you're using.

    If you're using Borland Pascal, Delphi or FreePascal, look here for details on calling C from Pascal (and vice versa):

    http://www.drbob42.com/delphi/headconv.htm

    But if you're using any of those three Pascal's, consider finding an existing Pascal "thread" API, instead of creating your own wrapper.