Link to home
Start Free TrialLog in
Avatar of ThievingSix
ThievingSixFlag for United States of America

asked on

C++ To Delphi Translation(Small Piece)

The attatched snipped is what I am trying to translate:

What I have so far is this and it likes to give an access violation:
const
  cmtNormal : Integer = 0;
  cmtSystem : Integer = 1;
  cmtBroadcast : Integer = 2;
  cmtEnd : Integer = 3;
  clCurrent : Integer = 0;
  clLobby : Integer = 1;
  clStage : Integer = 2;
  clGame : Integer = 3;
  clEnd : Integer = 4;

type
  TZChatOutput = procedure(Buffer: PChar; iType: Integer; iLoc: Integer; ChatColor: TColor);

var
  ZChatOutput : TZChatOutput;

procedure Echo(Output: String);

implementation

uses Addresses;

procedure Echo(Output: String);
var
  Buffer : PChar;
begin
  If Length(Output) < 127 Then
    begin
    If @ZChatOutput <> nil Then
      begin
      StrPCopy(Buffer,Output);
      ZChatOutput(Buffer,cmtSystem,clCurrent,$FFFFFFFF);
    end;
  end;
end;
#define ADDR_ZCHATOUTPUT	0x0042C660
//The definition of the Gunz output chat function
typedef void (__cdecl* ZChatOutputFunc)(const char* lpcMsg, int iType /*= 0*/,int iLoc /*= 0*/,  DWORD dwColor);
 
//Making the pointer to that func in Gunz
ZChatOutputFunc ZChatOutput = (ZChatOutputFunc)ADDR_ZCHATOUTPUT;
 
//Our own little interface function to it - works just like printf()
void Echo(const char* lpcFmt, ...){
	//Temp buffer
	char szBuf[0x4000];
 
	//Our args list
	va_list vaArgs;
 
	//Take whatevers in lpcFmt and make a real string out of it
	va_start(vaArgs, lpcFmt);
	_vsnprintf(szBuf, sizeof(szBuf), lpcFmt, vaArgs);
	va_end(vaArgs);
 
	//Now that szBuf holds the formatted string, let's dump it to the chat display
	ZChatOutput(szBuf, 1, 0, 0xFFFFFFFF);
}

Open in new window

Avatar of 2266180
2266180
Flag of United States of America image

first problem: callign convention:

type
  TZChatOutput = procedure(Buffer: PChar; iType: Integer; iLoc: Integer; ChatColor: TColor); cdecl; <--------- here

second problem, the ZChatOutput variable is not initialized.

var
  ZChatOutput : TZChatOutput = TZChatOutput(ADDR_ZCHATOUTPUT);

I don't know where ADDR_ZCHATOUTPUT is defined. I don't see it in the snipet.
Avatar of ThievingSix

ASKER

The code snippet works, it that I need to change to delphi.

I'm using the same calling convention that the c++ code does.

  If Reason = DLL_PROCESS_ATTACH Then
    begin
    ZChatOutput := Pointer(AddressZChatOutput);
    Echo('This is a dll test =)');
  end;

is also part of the delphi code that failed.
Everything is defined.
>> I'm using the same calling convention that the c++ code does.

no you're not.

typedef void (__cdecl*

you're delphi code

type
  TZChatOutput = procedure(Buffer: PChar; iType: Integer; iLoc: Integer; ChatColor: TColor);

where's the cdecl? in my comment, not your code -_-
TZChatOutput = procedure(Buffer: PChar; iType: Integer; iLoc: Integer; ChatColor: Cardinal);stdcall;

at which point you have AV, when calling ZChatOutput function?
btw. I can't see where you load DLL abd obtain pointer to function

ziolko.
ooops sorry guys I dodn't refresh webbrowser

ziolko.
@ciuly
Sorry, I guess I pasted when I was trying without the calling:
type
  TZChatOutput = procedure(Buffer: PChar; iType: Integer; iLoc: Integer; ChatColor: TColor); cdecl;
is what I have tried;

@ziolko
procedure EntryPoint(Reason: DWORD);
begin
  If Reason = DLL_PROCESS_ATTACH Then
    begin
    ZChatOutput := Pointer(AddressZChatOutput);
    Echo('This is a dll test =)');
  end;
end;

begin
  DLLProc := @EntryPoint;
  EntryPoint(DLL_PROCESS_ATTACH);
end.
And yes I AV when calling ZChatOutput.
I'll leave it to ciuly he was first and besides... it's hard to find better expert in this area:)

ziolko.
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect, didn't know it was so simple. Thankyou.