954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ A Few Errors

When i compile this code i get a few errors (i guess are simple for you guys)

void WriteMem(DWORD MemOffset, DWORD DataPtr, DWORD dataLen) {
  { DWORD OldProt;
  VirtualProtect((void*) MemOffset, dataLen, PAGE_EXECUTE_READWRITE, &OldProt); 
  RtlMoveMemory((void*) MemOffset, (const void*) DataPtr, dataLen);
  VirtualProtect((void*) MemOffset, dataLen, OldProt, &OldProt); 
  }
  void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len); {
   for(DWORD i = 0; i < len; i++) 
    WriteMem((DWORD)AddrToChange+i, (DWORD)To+i, 1); 
  }


Errors

--------------------Configuration: DLL - Win32 Debug--------------------
Compiling...
Main.cpp
error C2601: 'WriteMem' : local function definitions are illegal
error C2601: 'Initialize' : local function definitions are illegal
error C2601: 'Shutdown' : local function definitions are illegal
error C2601: 'DllMain' : local function definitions are illegal
fatal error C1004: unexpected end of file found
Error executing cl.exe.
DLL.dll - 5 error(s), 0 warning(s)
Gunner54
Light Poster
37 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

>> { DWORD OldProt;

remove the '{' character.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

LOL im so dumb not to see that! i must be blind but... now i have these

--------------------Configuration: DLL - Win32 Debug--------------------
Compiling...
Main.cpp
error C2601: 'WriteMem' : local function definitions are illegal
error C2065: 'len' : undeclared identifier
warning C4018: '<' : signed/unsigned mismatch
error C2065: 'AddrToChange' : undeclared identifier
error C2065: 'To' : undeclared identifier
Error executing cl.exe.
DLL.dll - 4 error(s), 1 warning(s)
Gunner54
Light Poster
37 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

>> void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len); {

remove the semicolon

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Now i have like the most errors ever.

--------------------Configuration: DLL - Win32 Debug--------------------
Compiling...
Main.cpp
error C2065: 'MemOffset' : undeclared identifier
error C2065: 'dataLen' : undeclared identifier
error C2065: 'DataPtr' : undeclared identifier
error C2065: 'len' : undeclared identifier
warning C4018: '<' : signed/unsigned mismatch
error C2065: 'AddrToChange' : undeclared identifier
error C2065: 'To' : undeclared identifier
error C2143: syntax error : missing ';' before 'else'
error C2143: syntax error : missing ';' before '{'
error C2447: missing function header (old-style formal list?)
error C2143: syntax error : missing ';' before 'else'
error C2143: syntax error : missing ';' before '{'
error C2447: missing function header (old-style formal list?)
error C2143: syntax error : missing ';' before 'else'
error C2143: syntax error : missing ';' before '{'
error C2447: missing function header (old-style formal list?)
error C2143: syntax error : missing ';' before 'else'
error C2143: syntax error : missing ';' before '{'
error C2447: missing function header (old-style formal list?)
error C2143: syntax error : missing ';' before 'else'
error C2143: syntax error : missing ';' before '{'
error C2447: missing function header (old-style formal list?)
error C2143: syntax error : missing ';' before 'else'
error C2143: syntax error : missing ';' before '.'
error C2501: 'ZChat__InputDet' : missing storage-class or type specifiers
error C2371: 'ZChat__InputDet' : redefinition; different basic types
see declaration of 'ZChat__InputDet'
error C2143: syntax error : missing ';' before '.'
error C2143: syntax error : missing ';' before 'return'
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '{'
error C2447: missing function header (old-style formal list?)
error C2065: 'Initialize' : undeclared identifier
Error executing cl.exe.
DLL.dll - 33 error(s), 1 warning(s)



My Code

void WriteMem(DWORD MemOffset, DWORD DataPtr, DWORD dataLen); {
  DWORD OldProt;
  VirtualProtect((void*) MemOffset, dataLen, PAGE_EXECUTE_READWRITE, &OldProt); 
  RtlMoveMemory((void*) MemOffset, (const void*) DataPtr, dataLen);
  VirtualProtect((void*) MemOffset, dataLen, OldProt, &OldProt); 
  }
  void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len);
   for(DWORD i = 0; i < len; i++) 
    WriteMem((DWORD)AddrToChange+i, (DWORD)To+i, 1); 
  }
Gunner54
Light Poster
37 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

that's what happens then you fix something :cheesy: look at the first eror message and try to find out why MemOffset is undefined. Where did you define it? It must be in one of two places: (1) a global variable, or (2) a variable declared inside the function in which its used.

Fix one error and it may solve several error messages.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

First you have this:

void WriteMem(DWORD MemOffset, DWORD DataPtr, DWORD dataLen) {
...
  }
  void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len); {
   for(DWORD i = 0; i < len; i++) 
    WriteMem((DWORD)AddrToChange+i, (DWORD)To+i, 1); 
  }

and the recomendation was>> void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len); {
remove the semicolon

Now you have:

void WriteMem(DWORD MemOffset, DWORD DataPtr, DWORD dataLen); {


What changed in the line (and more importantly, why did it change?)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

and first you have this:

void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len); {
   for(DWORD i = 0; i < len; i++) 
    WriteMem((DWORD)AddrToChange+i, (DWORD)To+i, 1); 
  }

and now:

void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len);
   for(DWORD i = 0; i < len; i++) 
    WriteMem((DWORD)AddrToChange+i, (DWORD)To+i, 1); 
  }

which are both wrong. Is this the implementation of the function or are your just trying to call it and is it part of something else?

regards Niek

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

im trying to make the EnableHack function (its so i can edit a processes memory)

Gunner54
Light Poster
37 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

ok, then you should remove the semicolon as Ancient Dragon allready said.

void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len) 
  {
   for(DWORD i = 0; i < len; i++) 
    WriteMem((DWORD)AddrToChange+i, (DWORD)To+i, 1); 
  }


And in your other function too offcourse, as WaltP pointed out.

regards Niek

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Yay! i got it to work now for an easy part

void WriteMem(DWORD MemOffset, DWORD DataPtr, DWORD dataLen) {
  DWORD OldProt;
  VirtualProtect((void*) MemOffset, dataLen, PAGE_EXECUTE_READWRITE, &OldProt); 
  RtlMoveMemory((void*) MemOffset, (const void*) DataPtr, dataLen);
  VirtualProtect((void*) MemOffset, dataLen, OldProt, &OldProt); 
  }
  void EnableHack(BYTE* AddrToChange, BYTE* To, DWORD len) 
  {
   for(DWORD i = 0; i < len; i++) 
    WriteMem((DWORD)AddrToChange+i, (DWORD)To+i, 1); 
  }
 
DWORD ZChat__Input = ADDR_ZCHATINPUT;
 
CDetour ZChat__InputDet;
bool __stdcall ZChat__InputHook(const char* lpcLine){
  bool bRet = true;
 
 if(stricmp(lpcLine, "!god") == 0){
  bRet = false;
  if(ADDR_GODMODE[0] != 0x90){
        BYTE godmode[] = {0x90, 0x90};
        EnableHack((BYTE*)ADDR_GODMODE, godmode, 2);
        Echo("GodMode On");
        }
        else
        BYTE godmode[] = {0x7B, 0x05};
        EnableHack((BYTE*)ADDR_GODMODE, godmode, 2);
        Echo("GodMode Off");
        }


Errors

--------------------Configuration: DLL - Win32 Debug--------------------
Compiling...
Main.cpp
error C2109: subscript requires array or pointer type
error C2065: 'godmode' : undeclared identifier
Error executing cl.exe.
Creating browse info file...
DLL.dll - 2 error(s), 0 warning(s)
Gunner54
Light Poster
37 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 
error C2065: 'godmode' : undeclared identifier


You are declaring "godmode" inside an if-statement, this means it won't be visible outside it. Just declare it at the beginning of the function.

regards Niek

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

You will see the problem immediately if you format your code better:
Put each { and } on separate lines
Every line between corresponding { }'s should be indented 3-4 SPACEs.

For example:

int function(int val)
{
    int rtn, tmp;
    if (val > 10)
    {
        tmp = val * 4;
        rtn = tmp + val;
    }
    else
    {
        tmp = val * 6;
        rtn = tmp - val;
    }
    return rtn;
}

Errors

--------------------Configuration: DLL - Win32 Debug--------------------
Compiling...
Main.cpp
error C2109: subscript requires array or pointer type
error C2065: 'godmode' : undeclared identifier
Error executing cl.exe.
Creating browse info file...
DLL.dll - 2 error(s), 0 warning(s)


Please let us know what lines these error are on. Just telling us the errors does not give us enough information (although I did find one -- this time)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
You are declaring "godmode" inside an if-statement, this means it won't be visible outside it. Just declare it at the beginning of the function. regards Niek



I dont understand "Just declare it at the beginning of the function." what function the WriteMem of the ZChat::Input Hook?

sry im not that good at C++

Gunner54
Light Poster
37 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

just after bool bRet = true;
Where did you get this code anyway? Because I'm guessing you didn't write it youself? Be sure to read and understand WaltP's post because you'll encouter a lot more problems related to this...

regard Niek

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
int function(int val)
{
    int rtn;
    if (val > 10)
    {
        int tmp = val * 4;
        rtn = tmp + val;
    }
    else
    {
        tmp = val * 6;
        rtn = tmp - val;
    }
    return rtn;
}


In this version of WaltPs example rtn is visible/available throughout the function whereas tmp is only visible/available in the body of the if and is not visible/available in the body of the else. This is what's happening in the code you posted.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

The function is reduced to this:

int function(int val)
{
    return (val * 5);
}


why? because of the associative (?) properties of mathametics

val * 4 + val
is the same as
val + val + val + val + val; // there are 5 vals on this line

val * 6 - val;
is the same as this
val + val + val + val + val + val - val;
which is
val + val + val + val + val; // there are also 5 vals on this line

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Guys, I was showing formatting techniques to aid the poster. Multiple posts explaning the code is not helpful in understanding the formatting:
You will see the problem immediately if you format your code better:
Put each { and } on separate lines
Every line between corresponding { }'s should be indented 3-4 SPACEs.

For example:...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You