473,387 Members | 1,379 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,387 software developers and data experts.

Question on Marshal of a unsigned short


I need to call into a C++ DLL from my C# code.
The function is expecting a void pointer to an unsigned short.
Which would be more correct?

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

or

IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(wRegData));
Marshal.WriteInt16(p, (short) wRegData);
Thank you.

Nov 26 '05 #1
2 8113

"scottt" <sc****@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...

I need to call into a C++ DLL from my C# code.
The function is expecting a void pointer to an unsigned short.
Which would be more correct?

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

or

IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(wRegData));
Marshal.WriteInt16(p, (short) wRegData);
Thank you.


ushort wRegData= 123;

fncExpectingPointerToUInt(ref wRegData);

Willy.
Nov 26 '05 #2
Thank you. so simple I over looked it.
"Willy Denoyette [MVP]" wrote:

"scottt" <sc****@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...

I need to call into a C++ DLL from my C# code.
The function is expecting a void pointer to an unsigned short.
Which would be more correct?

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

or

IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(wRegData));
Marshal.WriteInt16(p, (short) wRegData);
Thank you.


ushort wRegData= 123;

fncExpectingPointerToUInt(ref wRegData);

Willy.

Nov 28 '05 #3

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

Similar topics

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...
1
by: Paul Edwards | last post by:
I have the following C program: #include <stdio.h> int main(void) { unsigned short x = 32768, y=16384; if (x > y) {
8
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes ....
4
by: slougheed | last post by:
I encountered a problem after we had converted our declarations of 'unsigned short int' to uint16_t. In one instance, whoever did the conversion failed to delete the 'short' keyword so we had a...
5
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
0
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
1
by: wolverine | last post by:
Hi, Given below is a traits with unsigned short. If i create a stringstream with this traits will there be any problem. struct XMLCh_traits { typedef unsigned short char_type; typedef...
10
by: Jim Langston | last post by:
Is the following well defined? size_t IntVal = 65537; unsigned short Length; if ( IntVal static_cast<unsigned short>( -1 ) ) { std::cout << "Value too long to fit in a short" << std::endl;...
2
by: CptDondo | last post by:
I'm trying to write what should be a simple program, and it keeps hanging if I use volatile.... The program, stripped of its error checking, is this: unsigned short * start; unsigned short *...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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,...

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.