Object reference not set to an instance of an object.

G

Guest

I think is related to my p/invoke thing.

This is the C dll implementation. But i am kind of blur, what does it means?

#define far
#define near
#if ... // something here
#define pascal __stdcall
#else
#define pascal
#endif

typedef struct {
int aValue;
unsigned char rev[10];
} StructureA;

typedef int BOOL;

BOOL far *LPBOOL;

DLLAPI int 3rdPartyDecl GetData(int, StructureA far *)

--
When i right click and select reference the LPBOOL, it goes into:

WINADVAPI
BOOL
WINAPI
AccessCheck (
PSECURITY_DESCRIPTOR pSecurityDescriptor,
HANDLE ClientToken,
DWORD DesiredAccess,
PGENERIC_MAPPING GenericMapping,
PPRIVILEGE_SET PrivilegeSet,
LPDWORD PrivilegeSetLength,
LPDWORD GrantedAccess,
LPBOOL AccessStatus
);
--

When i call it in C:

int test;
StructureA myStructureA;

test = GetData(someIntVar, &myStructureA);

// GetData will pump value into myStructureA, and it works...

-----------------------------------------------------------------------------------

Any idea how to simulate this in C#?

I tried this in C# dll:

// either ways... but i still receive object reference problems...
[DllImport("kernel32.dll", EntryPoint="#1", SetLastError=true)]
private static extern int GetData(int someIntVar, ref StructureA
myStructureA);

or

[DllImport("kernel32.dll", EntryPoint="#1", SetLastError=true)]
private static extern int GetData(int someIntVar, out StructureA
myStructureA);

or

[DllImport("kernel32.dll", EntryPoint="#1", SetLastError=true)]
private static extern int GetData(int someIntVar, StructureA myStructureA);

i rewrite the StructureA in C#:

public struct StructureA
{
private int aValue;
private byte[] rev;

public int AValue
{
get { return aValue; }
set { aValue = value; }
}

public byte[] rev
{
get { return rev; }
set { rev = value; }
}

public StructureA(int testValue)
{
// create this cstor to initialize all variables
aValue = testValue;
rev = new byte[10];
}
}

When i calls in tester program:

StructureA myStructureA = new StructureA(0);
int testresult;

// i tried any of this 3 codes at a time
// 1
testresult = GetData(10, ref myStructureA); // die off here, with object
reference problem

// 2
testresult = GetData(10, out myStructureA); // die off here, with object
reference problem

// 3
testresult = GetData(10, myStructureA); // die off here, with object
reference problem

I just don't understand why they had a far keyword inside the C Structure,
and how do i simulate this in C# p/invoke?

Any help please, thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top