Jim MacDiarmid
Jim MacDiarmid

Reputation: 149

How can I convert or translate C++ define macros so I can use them in C#

I have the following macros I'm trying to convert.

#define FIELD_OFFSET(type, field)    ((LONG)&(((type *)0)->field))
#define IMAGE_FIRST_SECTION(ntheader) ((PIMAGE_SECTION_HEADER) ((DWORD)ntheader + \ 
FIELD_OFFSET( IMAGE_NT_HEADERS, OptionalHeader ) + \
((PIMAGE_NT_HEADERS)(ntheader))->FileHeader.SizeOfOptionalHeader))

I'm pretty green with C++.. could someone give me a hand please?

Thanks in advance!

Upvotes: 1

Views: 500

Answers (1)

DanTheMan
DanTheMan

Reputation: 3277

c# doesn't support macros.

Just create them as functions. The only real advantage to macros in c++ is that you're forcing an inline. The compiler in C# will be smart enough to make the inline decision for you.

It's really hard to give you an example from the examples you gave me, mostly because they don't 'make sense' in c#, but I'll try to give you an idea:

#define AddNumbers(x,y)    x+y

can be translated to:

public Int32 AddNumbers( Int x, Int y )
{
    return x+y;
} 

EDIT:

The guys below had a point that I had simplified the conversion TOO much. My point was more to show the structure, not the actual conversion process.

Upvotes: 2

Related Questions