473,657 Members | 2,531 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.AllocC oTaskMem()". 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 1427
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.WriteIn t16(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.WriteIn t16(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.System DefaultCharSize == 1)
Marshal.WriteBy te(ptr, 0);
else
Marshal.WriteIn t16(ptr, 0);

of if you want to be really dynamic

byte[] zeros = new byte[Marshal.SystemD efaultCharSize];
Marshal.Copy(ze ros, 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.System DefaultCharSize == 1)
Marshal.WriteBy te(ptr, 0);
else
Marshal.WriteIn t16(ptr, 0);

of if you want to be really dynamic

byte[] zeros = new byte[Marshal.SystemD efaultCharSize];
Marshal.Copy(ze ros, 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.System DefaultCharSize " 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
8868
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 { mySocketClass sss; ...
16
2495
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 SRB_HaId;
1
1587
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 that I read from internet, I realized that InvokeMember works for methods in class, but not when I have an exe ( right ?). My program looks like this :
7
5393
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 filter-object without setting any properties. but the value type-properties can´t be null and the filter is set to 0 (int) or false (bool). therefore i did implement the propertySpecified-pattern like this:
15
62044
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
2650
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
12749
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 - I'd open up the service from my browser, select the appropriate function from the list, click the Invoke button and all worked well. One of the things I do each fiscal quarter is to make a change to the service, so it will return the correct...
2
4697
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 which one is better to prevent unpredictable update of label. Whether should i use lock statement or control.invoke and give method name. Please correct me if i am wrong.
3
3227
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 code is: Public Class MyClass1 Implements System.ComponentModel.ISynchronizeInvoke Private _InvokeRequired As Boolean = False Public Function BeginInvoke(ByVal method As System.Delegate, ByVal args() As Object) As...
2
2495
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 client by passing the connection details. I request anyone can suggest me the approach in meeting the requirement. Thanks in advance. Regards
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.