473,398 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Help Porint C++ code to C#

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().


Nov 17 '05 #1
8 1432
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
"scottt" <sc****@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
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().



Nov 17 '05 #2
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.
"Pete Davis" wrote:
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
"scottt" <sc****@discussions.microsoft.com> wrote in message
news:D4**********************************@microsof t.com...
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().




Nov 17 '05 #3
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
Nov 17 '05 #4

When adding the assert I get the following error compiling.

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


"Ashura" wrote:
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

Nov 17 '05 #5
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.Equ als(null));

and the assert happens so its not null.


"Ashura" wrote:
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

Nov 17 '05 #6
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;
} ;

Nov 17 '05 #7
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!

Nov 17 '05 #8
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.


"Ashura" wrote:
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;
} ;

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
6
by: Mark Reed | last post by:
Hi all, I am trying to learn a little about programming (I know next to nothing so far) and have found some code which hides the toolbars. However, this bit of code is a little too effective and...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.