473,402 Members | 2,055 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,402 software developers and data experts.

C++ to C# with unsigned long *

Hi there,
I have problem with importing my C++ code to C#.

The c++ looks like these

extern _declspec(dllexport) const char*
SendAndReceiveBufferedWrp(__int64 hConnection, const char*
pchSendBuffer, int iCmdDataMode,unsigned long bufferLength, unsigned
long * pulNumBytesRcved) {
}

And the C#
[DllImport("\\dll\\Test.dll", EntryPoint =
"SendAndReceiveBufferedWrp")]
unsafe private static extern string
SendAndReceiveBufferedDll(Int64 connection, string sendBuffer,int
cmdDataMode,ulong bufferLength,ref ulong numbytes);
unsafe public string SendAndReceiveBuffered(Int64 connection,
string sendBuffer,int cmdDataMode,ulong bufferLength,out ulong
numbytes)
{
return SendAndReceiveBufferedDll(connection, sendBuffer,
cmdDataMode, bufferLength,ref numbytes);
}
in the c++ the pulNumBytesRcved pointer is allways NULL there is no
address passed.
Could somebody help me with thise ?

Thanks in advance.

Aug 8 '06 #1
3 36443
tu****@gmail.com wrote:
Hi there,
I have problem with importing my C++ code to C#.
The C++ type unsigned long is 32 bits - equivalent to System.UInt32.

The C# type ulong is 64 bits, equivalent to unsigned __int64.

Fix the declarations in the C# code and you should be okay.

-cd

Aug 8 '06 #2
Thanks I have got it mean while. But now I got a new problem I have
structure that is

typedef struct _tagEventstruct
{
unsigned long ulVersion;
__int64 hConnection;
unsigned long ulListener;
unsigned long ulType;
unsigned long ulId;
unsigned long ulSubClass;
unsigned long ulTimeStampSec;
unsigned long ulTimeStampMicroSec;
unsigned char* puchData;
unsigned long ulDataLength;
unsigned short* pwszDesc;
unsigned short wszModuleName[128];
unsigned long ulModuleLineNumber;
#ifndef SWIG
void** ppData;
#else
unsigned long ppData;
#endif
}XdsEvent;

and in C# it is like
[StructLayout(LayoutKind.Sequential,CharSet=CharSet .Unicode)]
public struct XdsEvent
{
public uint ulVersion;
public Int64 hConnection;
public uint ulListener;
public uint ulType;
public uint ulId;
public uint ulSubClass;
public uint ulTimeStampSec;
public uint ulTimeStampMicroSec;
public IntPtr puchData;
public uint ulDataLength;
public IntPtr pwszDesc;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=128)]public
string wszModuleName;
public uint ulModuleLineNumber;
public IntPtr ppData;
}

It works fine for some data i.e. wszModuleName but some data is allways
null.

I am getting the pwszDesc string value by
string data = Marshal.PtrToStringUni(eventdata.pwszDesc);
and
string data = Marshal.PtrToStringAnsi(eventdata.puchData);

but they allways return null also some othere data like ulDataLength
return bad numbers.
At the moment I am also stuck with a function
in C++

xds_bool BroadcastMessageReceived( __int64 hConnection,
const
unsigned char* pData,
unsigned
long ulDataLength,
unsigned
long ulStatusCode,
const
unsigned short * wszDesc,
const
unsigned short * wszModuleName,
unsigned
long ulLineNumber )

where wszDesc and wszModuleName are null terminate Unicode strings

and in C#
[DllImport("\\XdsIII.dll", EntryPoint =
"BroadcastMessageReceived")]
private static extern bool BroadcastMessageReceivedDll(Int64
connection, IntPtr data, uint dataLength, uint statusCode, IntPtr desc,
IntPtr moduleName, uint lineNumber);
public bool BroadcastMessageReceived(Int64 connection, string
data, uint dataLength, uint statusCode, string description, string
moduleName, uint lineNumber)
{

return
BroadcastMessageReceivedDll(connection,Marshal.Str ingToHGlobalAnsi(data),
dataLength, statusCode, Marshal.StringToHGlobalUni(description),
Marshal.StringToHGlobalUni(moduleName), lineNumber);
}

but it keeps faling with AccessViolationException

Could you help me with these?

Aug 10 '06 #3
I solved it. The problem was that the structure defined in c++ was in
wrong order and C# did not import it correctly. Although what I dont
know is that is this a standard behaviour that some data is swithced
due to some BigEndien stuff ?!

Finally finished.

Aug 10 '06 #4

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

Similar topics

7
by: William Payne | last post by:
Hello, I have a variable of type unsigned long. It has a number of bits set (with set I mean they equal one). I need to determine those bits and their position and create new numbers from them. For...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
25
by: amit | last post by:
Hi, What will happen in following scenario.. long a = -2; longlong b = a long c; c = *(ulonglong *)&b; b = c; also when
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
9
by: luke | last post by:
Hi everybody, please, can someone explain me this behaviour. I have the following piece of code: long long ll; unsigned int i = 2; ll = -1 * i; printf("%lld\n", ll);
14
by: moumita | last post by:
Hi All, I need to convert 4 bytes to an unsigned long. Suppose I have one array like unsigned char buf.I need to convert these 4 bytes into a single unsigned long. Is the following piece of code...
7
by: somenath | last post by:
Hi All, I am trying to undestand "Type Conversions" from K&R book.I am not able to understand the bellow mentioned text "Conversion rules are more complicated when unsigned operands are...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
13
by: Martin Wells | last post by:
I commonly use the likes of -1 for max unsigned values, or something like -7 to get 6 away from the max value; sort of like: unsigned x = ... if (-7 == x) puts("x is 6 away from its maximum");...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.