DllCall Error with MCode Function

Get help with using AutoHotkey and its commands and hotkeys
User avatar
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

DllCall Error with MCode Function

17 Nov 2013, 07:38

Im getting the Error 0xc0000005 ("access violation") when i execute this code:

Code: Select all

/*
int MyFunction(float abc)
{
  return abc/50;
}
*/
MyFunction := MCode("2,x86:2UQkBNw1AAAAAOkAAAAA,x64:8w9eBQAAAADzDyzAww==")
Msgbox % DllCall(MyFunction,"float",50.5,"cdecl")
But not when I execute this code:

Code: Select all

/*
int MyFunction(int abc)
{
  return abc/50;
}
*/
MyFunction := MCode("2,x86:uB+F61H3bCQEwfoEi8LB6B8DwsM=,x64:uB+F61H36cH6BIvCwegfA8LD")
Msgbox % DllCall(MyFunction,"int",50,"cdecl")
Recommends AHK Studio
User avatar
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: DllCall Error with MCode Function

17 Nov 2013, 07:47

Is it caused by the FPU ?
Recommends AHK Studio
lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: DllCall Error with MCode Function

18 Nov 2013, 03:19

Do you have an asm listing or disassembly?
User avatar
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: DllCall Error with MCode Function

18 Nov 2013, 05:43

If I complile it on my PC the ASM code is also written down in the file.
I'll look post the contents later.
Recommends AHK Studio
User avatar
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: DllCall Error with MCode Function

19 Nov 2013, 13:29

OK here is the ASM Code.
Sorry for the delay.

Code: Select all

00000000  D9442404          fld dword [esp+0x4]
00000004  DC3500000000      fdiv qword [dword 0x0]
0000000A  E900000000        jmp dword 0xf
Using ndisasm (NASM disassembler).
The jmp seems weird to me.

Edit here is the ASM-Code of the second example:

Code: Select all

00000000  B81F85EB51        mov eax,0x51eb851f
00000005  F76C2404          imul dword [esp+0x4]
00000009  C1FA04            sar edx,byte 0x4
0000000C  8BC2              mov eax,edx
0000000E  C1E81F            shr eax,byte 0x1f
00000011  03C2              add eax,edx
00000013  C3                ret
Recommends AHK Studio
lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: DllCall Error with MCode Function

19 Nov 2013, 15:55

The jmp is harmless - it jumps 0 bytes, to address 0xf, immediately after the jmp instruction. However, it should be followed by a ret instruction.

Memory operands are enclosed in [square brackets]. [dword 0x0] is not a valid address to dereference, hence the Access Violation. It should be the absolute (non-portable) address of a 64-bit floating-point number.
lexikos
Posts: 6488
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: DllCall Error with MCode Function

20 Nov 2013, 05:33

Disassembling the compiled code isn't as useful as having the compiler output an asm listing in the first place. When I compile your first function with VC and the switches /FAc /Fa"code.cod", code.cod contains the following:

Code: Select all

code.c
_MyFunction PROC					; COMDAT
  00000	d9 44 24 04	 fld	 DWORD PTR _abc$[esp-4]
  00004	dc 35 00 00 00
	00		 fdiv	 QWORD PTR [email protected]
  0000a	e9 00 00 00 00	 jmp	 __ftol2_sse
_MyFunction ENDP
jmp is supposed to jump to the intrinsic function __ftol2_sse, but the jump offset hasn't been filled in. It can't be filled in; this code can't work for your purposes because the offset depends on where in memory the compiled code ends up relative to the intrinsic function. The function call is needed to convert the float to the return type required by your function (int). jmp is being used in place of call and ret, so ret is not needed.
User avatar
nnnik
Posts: 4242
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: DllCall Error with MCode Function

20 Nov 2013, 08:38

Well then I'll just have to use the /QIfist option.
But that doesn't solve the 0 address pointer problem or?
Recommends AHK Studio

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Bing [Bot], Google [Bot] and 182 guests