473,503 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trivial P/Invoke question

Hi there,

I need to initialize the first character to (binary) zero in a buffer
returned via "Marshal.AllocCoTaskMem()". This is ultimately going to be
assigned to an LPTSTR pointer inside a Win32 structure (for subsequent
passing to a Win32 function). Can someone show me how to actually assign
this zero (C null terminator) from the "IntPtr" returned from the latter
function (with emphasis on the fact that I'm dealing with a TCHAR from
Win32 - could be one or two characters that need to be zeroed IOW) . Thanks.
May 4 '07 #1
4 1417
Larry,
>This is ultimately going to be
assigned to an LPTSTR pointer inside a Win32 structure (for subsequent
passing to a Win32 function).
In that case, wouldn't it be easier to use a string?

>Can someone show me how to actually assign
this zero (C null terminator) from the "IntPtr" returned from the latter
function (with emphasis on the fact that I'm dealing with a TCHAR from
Win32 - could be one or two characters that need to be zeroed IOW) . Thanks.
If you know that the buffer is always at least two bytes large, the
easiest way is

Marshal.WriteInt16(ptr, 0);
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 4 '07 #2
Thanks for the feedback.
Larry,
>>This is ultimately going to be
assigned to an LPTSTR pointer inside a Win32 structure (for subsequent
passing to a Win32 function).

In that case, wouldn't it be easier to use a string?
Yes but can you. I downloaded some sample code from a MSFT developer who
relies on low-level techniques for this. He seems to imply by one of his
comments that you need to rely on low-level methods. I'd much rather rely on
"string" of course but how?
>>Can someone show me how to actually assign
this zero (C null terminator) from the "IntPtr" returned from the latter
function (with emphasis on the fact that I'm dealing with a TCHAR from
Win32 - could be one or two characters that need to be zeroed IOW) .
Thanks.

If you know that the buffer is always at least two bytes large, the
easiest way is

Marshal.WriteInt16(ptr, 0);
Ok, but in theory I don't know if it will always be two bytes. I can't
assume an integer either (2 bytes or otherwise). While it will work for
Unicode on Win32, tt's really the OS's native "char" I need to work with
(whatever its size is) so a neutral way is required. If you have a Win32
background then it's whatever TCHAR natively maps to..
May 4 '07 #3
>Yes but can you. I downloaded some sample code from a MSFT developer who
>relies on low-level techniques for this. He seems to imply by one of his
comments that you need to rely on low-level methods. I'd much rather rely on
"string" of course but how?
It doesn't always work, but you can easily try it simply by changing
the field type from IntPtr to string.

>Ok, but in theory I don't know if it will always be two bytes. I can't
assume an integer either (2 bytes or otherwise). While it will work for
Unicode on Win32, tt's really the OS's native "char" I need to work with
(whatever its size is) so a neutral way is required. If you have a Win32
background then it's whatever TCHAR natively maps to..
Well sizeof(TCHAR) can only be one or two bytes on all current
platforms that I know of, so by always writing two bytes you ensure
that at least as many bytes as needed are zeroed out.

But you can certainly take the native char size into account if you
want.

if (Marshal.SystemDefaultCharSize == 1)
Marshal.WriteByte(ptr, 0);
else
Marshal.WriteInt16(ptr, 0);

of if you want to be really dynamic

byte[] zeros = new byte[Marshal.SystemDefaultCharSize];
Marshal.Copy(zeros, 0, ptr, zeros.Length);
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 4 '07 #4
Yes but can you. I downloaded some sample code from a MSFT developer who
>>relies on low-level techniques for this. He seems to imply by one of his
comments that you need to rely on low-level methods. I'd much rather rely
on
"string" of course but how?

It doesn't always work, but you can easily try it simply by changing
the field type from IntPtr to string.

>>Ok, but in theory I don't know if it will always be two bytes. I can't
assume an integer either (2 bytes or otherwise). While it will work for
Unicode on Win32, tt's really the OS's native "char" I need to work with
(whatever its size is) so a neutral way is required. If you have a Win32
background then it's whatever TCHAR natively maps to..

Well sizeof(TCHAR) can only be one or two bytes on all current
platforms that I know of, so by always writing two bytes you ensure
that at least as many bytes as needed are zeroed out.

But you can certainly take the native char size into account if you
want.

if (Marshal.SystemDefaultCharSize == 1)
Marshal.WriteByte(ptr, 0);
else
Marshal.WriteInt16(ptr, 0);

of if you want to be really dynamic

byte[] zeros = new byte[Marshal.SystemDefaultCharSize];
Marshal.Copy(zeros, 0, ptr, zeros.Length);
Mattias
Ok, thanks for the help. After severals hours of research/experimentation I
think I finally have it figured out now. Your reference to
"Marshal.SystemDefaultCharSize" solves the last piece of the puzzle. Thanks
again.
May 4 '07 #5

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

Similar topics

3
8850
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form {...
16
2469
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
1
1576
by: RickDee | last post by:
Hi. I need help badly here. I need to write a program to invoke methods ( static, public, nonstatic, with parameters, and etc ) from an exe file, but I keep facing problem. From all the notes...
7
5365
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i don´t want to filter the return value of the method, i have to pass a new instance of the...
15
61951
by: Oleg Subachev | last post by:
I need to programmatically invoke from other class Click event of the Button on my Form. Button.OnClick method is protected, not public. How to perform this ? Oleg Subachev
4
2638
by: Edward Diener | last post by:
When I try to find out if a trivial event has handlers, by comparing the event to nullptr, I get compiler error C3918. So my natural question is how does one test a trivial event for handlers ?
5
12720
by: J.Evans.1970 | last post by:
Hi. I've inherited a Web Service from a developer who is no longer here. One of the purposes of the service was to clear the cache on the server side. Up until a few days ago, it worked fine -...
2
4690
by: archana | last post by:
Hi all, I am having application wherein i am using asynchronous programming and using callback i am updating one label control. My question is can i use lock statment to lock control. If yes...
3
3211
balabaster
by: balabaster | last post by:
I have a class that I want to make thread-safe and am investigating the ISyncronizeInvoke interface and wondering just what it will take to implement this interface. So far the basic concept of my...
2
2488
by: Vighneswar | last post by:
Hi All I am doing with a standalone application in .NET, where I have to invoke the Outlook / Outlook Express by passing account info (username, password, pop3 server, ... etc) and SQL Server...
0
7281
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
7334
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...
1
6993
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
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
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...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.