• Welcome to Valhalla Legends Archive.
 

n00b C++ Question

Started by Master_Nabo, February 24, 2005, 05:10 PM

Previous topic - Next topic

Master_Nabo

I am using VSC++.net2k3, and I was wondering how to use the DWORD type in C++.

I am porting some code over from C# in order to make a Dynamic Link Library in C++, and I discovered that my complier does not reconsize the DWORD as an identifier, and it didn't reconize ZeroMemory or CopyMemory for some random reason.


#pragma once

using namespace System;

namespace CBnetHashing
{
...
}

static void CBnetHashing::XSha1::HashData(System::Byte *inbuff, System::Int32 nLength, System::Byte *outbuff)
{
DWORD dhashbuff[128];
ZeroMemory(dhashbuff, 128*4);
if (nLength > 64)
throw new System::ArgumentException("Data cannot be more than 64 bytes");
CopyMemory(dhashbuff, inbuff, nLength);
int i;
DWORD a, b, c, d, e, f;...
}


(the dots represents code that I have yet to finish.)

Basically, what I am trying to do is declair an array of the DWORD type (was used in C# code) into C++, ZeroMemory all of the addresses, and CopyMemory from inbuff to dhasbuff to a certain length.

UserLoser.


typedef unsigned long DWORD;


or try including header files such as windows.h.

Master_Nabo

Thanks, I can't believe I forgot the header file.

In the C# code, it refers to static functions, yet when I compile them, I get errors.

namespace CBnetHashing
{
public __gc class XSha1
{
public:
static void HashData(System::Byte *inbuff, System::Int32 nLength, System::Byte *outbuff);
private:
static System::UInt32 Rol(System::UInt32 n, int shift);
};


How can I fix this (I don't remember hearing about static function)?

UserLoser.


Master_Nabo

error C2724: 'CBnetHashing::XSha1::Rol' : 'static' should not be used on member functions defined at file scope

EpicOfTimeWasted

DWORD should actually be:

typedef unsigned int DWORD;

This makes sure that your double word doesn't turn into a quad word when it's compiled for a 64 bit CPU.

Kp

Quote from: EpicOfTimeWasted on February 24, 2005, 06:18 PM
DWORD should actually be:

typedef unsigned int DWORD;

This makes sure that your double word doesn't turn into a quad word when it's compiled for a 64 bit CPU.

Technically, on a 64bit CPU, a doubleword should be 128 bits.  Of course, it's rare you ever see a Windows programmer using the right definition of a machine word anyway. ;)
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Master_Nabo

Thanks for the info but...

Where do I put that?  When I include it before the name, my compiler says that:

error C2146: syntax error : missing ';' before identifier 'dhashbuff'

So could you post an example of making an array of DWORD?

From what I can make sence, it tries to make DWORD an interger, and wants to throw the rest of the declaration away. 

Kp

DWORD dw[5] /* big enough for a SHA output */;
DWORD *pdw = new DWORD[10] /* this line must be in a function, not at file scope */;
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

MyndFyre

A couple thoughts....

You definitely didn't see a "DWORD" type in a C# library.  The only "DWORD" type in a C# library would be System.UInt32 (System::UInt32), which is accessible through MC++, which brings me around to my next question, which is -- if you're using OTHER MC++ types like System::Byte, why aren't you using System::UInt32 instead of DWORD?

Also, the C# "static" keyword means that you don't need to have an instance of an object to access that particular member.  As I recall, in C++, you don't have to have an instance of an object, but you'll get an exception for trying to access a NULL pointer if the function you call uses the this pointer at all.  I could be wrong on that, though.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Master_Nabo

Nah, my project partner thought that DWORD was the C++ equivelent of the C# uint, which it isn't (maybe I should have thought more into this before posting, oh well).

Sure, I'll try and use System::UInt32 instead (this is what I originally planed to use, but Shout tried to convince me otherwise).


R.a.B.B.i.T


shout

He is porting my BnetHashing.dll to C++, which is available in C# here.

LordVader

#13
This page, talks about general issues when trying to compile C# like code under C++ and general idea's on conversions & fixes.

Most of the time it will just require (TYPE) cast infront of an instruction|function..
Where -> TYPE <- would be DWORD|INT etc.. or other 'type' of unit or structure known to the compiler.

Example from the page I mentioned.

In C this will work:

HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);

But in C++ requires a cast:

HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);