I have problem calling function in dll which was written in assembly. I've got this error:
EAccessViolation is raised when an application attempts to access a memory address for which there is no virtual memory allocated to the application.

Any idea?
Posted on 2002-03-05 10:53:31 by dJed_mRaz
Actually i pass one parameter to that procedure. If i just call procedure which doesn't have any parameter everything is ok.
Posted on 2002-03-05 10:55:17 by dJed_mRaz
We can't solve your problem by reading your mind, you are going to have to post some code. Sure, we could make educated guesses, but that is all they will be - guesses :)

You should post your line of pascal that calls the function, and the first couple of lines of the proc you are trying to call, that should get us started.
Posted on 2002-03-05 15:04:32 by sluggy
procedure TForm1.Button3Click(Sender: TObject);
begin
if not hoo2 then
begin
InstallHooks(form1.handle);
hoo2:=true;
end
else
begin
UninstallHooks;
hoo2:=false;
end;
end;


InstallHooks proc hdlg:DWORD
push hdlg
pop hDlg
invoke CreateThread, NULL, NULL, offset TrdProc, NULL, 0, ADDR ThreadID
invoke CloseHandle, eax
invoke SetWindowsHookEx,WH_GETMESSAGE,ADDR GetMSGProc,hInstance,0
mov hMSGHook,eax
invoke SetWindowsHookEx,WH_CBT,addr CBTProc,hInstance,NULL
mov hCBTHook,eax
ret
InstallHooks endp
Posted on 2002-03-05 16:05:01 by dJed_mRaz
Are you stepping through or running this code while in an IDE? I quote the following from MSDN:

Windows NT/2000: Closing an invalid handle raises an exception when the application is running under a debugger.


As you do not check the return value from your call to CreateThread, you may have an invalid handle to close. Try using some debug macros to monitor the steps your app takes, and the return values from calls.
Posted on 2002-03-05 18:34:17 by sluggy
Everything is ok with my createthread function, because it works when i call dll from my application writen in assembly. But it doesn't work when i call it from delphi.
Posted on 2002-03-08 11:14:30 by dJed_mRaz
Everything is ok with my createthread function,
You simply do not know that the CreateThread function call is okay when you call your function from delphi, as you never test the return from the CreateThread call to see if a valid handle is returned. If you are going to do API calls, then you must check returns to see if the call succeeded.

A problem with calling from delphi is that it uses a different parameter passing mechanism to C. Pascal passes the parameters on the stack in left-to-right order, and the caller must clean up the stack. IIRC, delphi uses the FastCall mechanism, which is the same as what i just explained (someone will correct me if i am wrong). As you are passing only one parameter, the left-to-right business will not matter. You will also find that delphi inserts the appropriate stack clean-up functionality everytime you call a function.

Here is what i suggest: insert a debug output line after every line of code in your asm function. You need to know what the value of hdlg is when you get it (before you push it). This is to check that you are sending the value of that handle, and not accidentally sending a pointer to it. You also need to check the return value (returned handle) from the CreateThread call. And while you are at it, you also need to check the handles returned from your two function calls that set hooks, they could be failing and you would never know.

To achieve what i suggested, pick up a debug macro lib from someone (there are a couple floating around, try Ernie's or vkim's), and insert the debug out lines, you may want to print them to a file.
Posted on 2002-03-09 06:37:53 by sluggy