Solved

Detour, "Inline" asm. Access Violation. C++ to Delphi translation

Posted on 2009-07-10
2
347 Views
Last Modified: 2013-11-05
This one is driving me bananas as it works in C++ and not in Delphi when to my eyes everything is the same. It's probably the way C++ handles variables from classes.

Anyway this is a small section for what I have:

  If Not(bNoRegs) Then
    begin
    Buffer.BufferAdd(Byte($89));
    Buffer.BufferAdd(Byte($0D));
    Buffer.BufferAdd(DWORD(@pClassInstance));
  end;

This is what it is in c++:

      if(!m_bNoRegs)
      {
            Buffer += (BYTE)0x89; //mov dword ptr, ecx
            Buffer += (BYTE)0x0D;
            Buffer += (DWORD)&m_pClassInstance;
      }

The Buffer variable isn't the issue as when the asm is written from the buffer it comes out right.
Now when each class is made the pClassInstance is set to nil(NULL in c++) it is a Pointer(BYTE * in c++). Now what is supposed to happen is pClassInstance is supposed to be set to ECX.
I.E. If ECX is $5E0000 pointer then pClassInstance should be equal to that. The same thing as doing pClassInstance := Pointer($5E0000);.

Now when this is executed on the delphi side I get an AV about not being able to access it.

Any ideas? If you need any other parts of the code just ask.
0
Comment
Question by:ThievingSix
2 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 24821760
I don't know Delphi but in C/C++

 (DWORD)&m_pClassInstance;

means

that the address of the pointer variable was pushed to the buffer and not the address the pointer was pointing to. That normally is a way to specify an output argument cause the callee might change the pointer value that way and the caller will get the new address assigned to m_pClassInstance.

0
 
LVL 13

Author Comment

by:ThievingSix
ID: 24821889
"Yeh thats what I assumed it was. MOV DWORD PTR DS:[&m_pClassInstance] essentially dereferences the pointer. Since pClassInstance is pointing to nothing it's value is 0. When I debugged the program and stepped through the asm it was indeed the pointer to the variable and not what the pointer was pointing to. I still don't understand why the"

That was as far as I got in my reply until I realized why the variable couldn't be accessed. The code section:
           
            Buffer += (BYTE)0x89; //mov dword ptr, ecx
            Buffer += (BYTE)0x0D;
            Buffer += (DWORD)&m_pClassInstance;

It never got EXECUTE privileges!  Thanks to you confirming what I did was right it dawned on me! Thanks.
 
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamically Created Query 3 62
How to build JSON File in Delphi 6 3 46
Wincontrol not (correctly) drawn 15 42
Firemonkey allowing RTL on android 6 47
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

828 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