473,471 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using PInvoke with string pointers

I need to use functions through PInvoke which accept and return pointers to
strings. What is the best way to get string pointers (IntPtr I am
guessing?) to pass into these functions, and the best way to take the
pointers (again, IntPtr I assume?) into strings?

Thanks,

Bob
Nov 16 '05 #1
2 7244
This is one area where there are so many ways to get the job done that it
gets confusing. In short, you can use the MarshalAsAttribute or
Marshal.StringToHGlobalAnsi method or the related or its related
StringToHGlobalUni and StringToHGlobalAuto methods.

To get the string back from the pointer, there are corresponding
PtrToStringAnsi, PtrToStringUni, and PtrToStringAuto methods.

To pass a string buffer that will be changed by the PInvoke call and
returned as a parameter to the called function, pass a Text.StringBuilder.

Different methods of passing the pointer have different memory management
requirements for freeing up the memory after the string, and the pointer to
it, is no longer needed. Make sure you check the memory management sections
in any MSDN articles on these methods.

HTH

DalePres
MCAD, MCDBA, MCSE
"Bob Dankert" <bo*@nospamnvsn-it.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
I need to use functions through PInvoke which accept and return pointers to
strings. What is the best way to get string pointers (IntPtr I am
guessing?) to pass into these functions, and the best way to take the
pointers (again, IntPtr I assume?) into strings?

Thanks,

Bob

Nov 16 '05 #2
Thanks a lot - this worked perfectly!

Bob

"DalePres" <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote in message
news:ur**************@TK2MSFTNGP14.phx.gbl...
This is one area where there are so many ways to get the job done that it
gets confusing. In short, you can use the MarshalAsAttribute or
Marshal.StringToHGlobalAnsi method or the related or its related
StringToHGlobalUni and StringToHGlobalAuto methods.

To get the string back from the pointer, there are corresponding
PtrToStringAnsi, PtrToStringUni, and PtrToStringAuto methods.

To pass a string buffer that will be changed by the PInvoke call and
returned as a parameter to the called function, pass a Text.StringBuilder.

Different methods of passing the pointer have different memory management
requirements for freeing up the memory after the string, and the pointer
to it, is no longer needed. Make sure you check the memory management
sections in any MSDN articles on these methods.

HTH

DalePres
MCAD, MCDBA, MCSE
"Bob Dankert" <bo*@nospamnvsn-it.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
I need to use functions through PInvoke which accept and return pointers
to strings. What is the best way to get string pointers (IntPtr I am
guessing?) to pass into these functions, and the best way to take the
pointers (again, IntPtr I assume?) into strings?

Thanks,

Bob


Nov 16 '05 #3

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

Similar topics

3
by: John Smith | last post by:
I wrote some code in C in a dll which I would like to call from C#. However I'm stuck because of the strongly typed behavior of C# which makes limitations. Here are the prototypes for two...
3
by: Charles Denny | last post by:
I'm trying to call CertFindCertificateInStore to find all certificates in the store that have the Code Signing enhanced key usage. I'm running into problems marshalling the array of OIDs in...
2
by: brian_harris | last post by:
I have a 3rd party unmanged .dll. This has functions that will look up information and fill a C string passed to it with data. I want to use this data in .NET classes after it has been filled and...
1
by: GTi | last post by:
I want to pInvoke a API (from a DLL) and it have the following C interface: callme(unsigned short, void*, void*, unsigned long, void*, unsigned char, char, unsigned char*); ...
7
by: Rymfax | last post by:
I would really appreciate it if someone could help me figure out what I'm doing wrong trying to PInvoke SetupDiEnumDriverInfo. All the other PInvokes i've done up to this point work fine. Whenver...
6
by: gregarican | last post by:
I am trying to port a legacy CTI application written in another programming language to C# 2005. From my initial research into it I see I can utilize the DllImport method to tap into the DLL file...
1
by: eastlands | last post by:
I need to use an unmanaged c++ dll which uses structs that contain callbacks and also functions. I have included the appropriate c++ definitials and my c# translations below. I first defined the...
0
by: emitojleyes | last post by:
Hi everyone! i'm new at this forum as well as programming in .net I have to develop a module that can use and configure a fingerprint detector; its documentation is written in C, as you will see...
4
by: Sagaert Johan | last post by:
Hi I am struggling with pinvoke I have dll a function that is declared as : sf_command (IntPtr sndfile,int command, IntPtr data, int datasize); i need to pass a pointer to a double in the...
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
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...
1
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
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...
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
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
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
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 ...

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.