Help Porint C++ code to C#

G

Guest

Need help porting code.
I have a section of code that is getting an Null exception error
and I want to know if I have ported the function correctly.
The code works in C++ but when running the code in C# I get a
System.NullReferenceException.


Thanks,


C++ Code:

typedef struct _VENDOR_CMD
{
BYTE bRequest;
WORD wValue;
WORD wIndex;
WORD wLength;
BYTE direction;
BYTE bData;
} VENDOR_CMD, *PVENDOR_CMD;


typedef struct _TT_SEND_VENDOR_CMD
{
VENDOR_CMD vcCmd;
DWORD dwDataSize;
PVOID pDataBuf;
} TT_SEND_VENDOR_CMD, *P_SEND_VENDOR_CMD;



BOOL MyClass::myFunction(PVENDOR_CMD pRequest, DWORD dwDataSize, PVOID
pDataBuf)
{
TT_SEND_VENDOR_CMD ttCmd;
memcpy(&ttCmd.vcCmd, pRequest, sizeof(VENDOR_CMD));
ttCmd.dwDataSize = dwDataSize;
ttCmd.pDataBuf = pDataBuf;

return(*pfnIpSysCall)(2, 0, &tjCmd);
} // End myFunction().





C# Code:


struct VENDOR_CMD {
public byte bRequest;
public Int16 wValue;
public Int16 wIndex;
public Int16 wLength;
public byte direction;
public byte bData;
};


struct TT_SEND_VENDOR_CMD {
public VENDOR_CMD vcCmd;
public UInt32 dwDataSize;
public Int32 pDataBuf;
} ;


private bool myfuunction(VENDOR_CMD pRequest, UInt32 dwDataSize, ref byte
pDataBuf) {
bool returnCode = false;

TT_SEND_VENDOR_CMD ttCmd;

ttCmd.vcCmd = pRequest;
ttCmd.dwDataSize = dwDataSize;
ttCmd.pDataBuf = pDataBuf;
//
// Exception Here when calling function:
//
// An unhandled exception of type 'System.NullReferenceException' occurred
in tttest.exe
//
// Additional information: Object reference not set to an instance of an
object
//
returnCode = IpSysCall(2, 0, ref ttCmd);

return(returnCode);
} // End myfunction().
 
P

Pete Davis

Well, from the looks of it, IpSysCall is null. But you don't show the code
relating to it, so I can't be positive.

What is IpSysCall?

Pete
 
G

Guest

IpSysCall is a function into a C++ DLL

I use import to use the function.
I don't think that IpSysCall is NULL since I call it several times
before it reaches this section of code.


Thank you.
 
G

Guest

i cudnt spot anything that is fundamentally wrong in the code. However i do
hav a bit of suspicion about the request cmd passed in.
try this and see wat happens next

System.Diagnostics.Debug.Assert(pRequest != null);

Hope this helps
 
G

Guest

When adding the assert I get the following error compiling.

Operator '!=' cannot be applied to operands of type 'tttest.TT.VENDOR_CMD'
and '<null>'
 
G

Guest

Oh and I should add that in the debugger I see the ttCmd.vcCmd is filled in
with data.

My thought was that the ttCmd.pDataBuf was not being assinged correctly.

so I added this:

System.Diagnostics.Debug.Assert(ttCmd.pDataBuf.Equals(null));

and the assert happens so its not null.
 
G

Guest

Well, as ya pointed out the "IpSysCall is a function into a C++ DLL"
try to change the memory layout of the struct to make it exactly the same as
c++ version

[StructLayout(LayoutKind.Sequential)]
struct VENDOR_CMD {
public byte bRequest;
public Int16 wValue;
public Int16 wIndex;
public Int16 wLength;
public byte direction;
public byte bData;
};

[StructLayout(LayoutKind.Sequential)]
struct TT_SEND_VENDOR_CMD {
public VENDOR_CMD vcCmd;
public UInt32 dwDataSize;
public Int32 pDataBuf;
} ;
 
D

darwinye

ttCmd.pDataBuf = pDataBuf;

pDataBuf is a reference to the byte type data buffer,so after this
assignment, ttCmd.pDataBuf's value is the value of the pDataBuf's ref
value.. so after the assignment ,the value of ttCmd.pDataBuf may be
equal to zero.
I think the may lead to null reference error!
 
G

Guest

Hi All,

Thanks for all your help. I found the answers and will summarize here:

1) You need "[StructLayout(LayoutKind.Sequential)]" around the structure to
prevent it from rearranging the variables inside the structure. Although in
VS.NET the structures still look like they are rearranged.

2) the void pointers in the structure become IntPtr.

3) In the routine I did the following

IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(ttCmd));
Marshal.StructureToPtr(ttCmd, p, false);

used the structure when calling the C++ function in the DLL

Marshal.FreeHGlobal(p);

to free the memory allocated.



Again thanks for all the help.
 

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