Is it possible to get a char*
for a string
variable in C#?
I need to convert a path string to a char*
for using some native win32 function ...
Is it possible to get a char*
for a string
variable in C#?
I need to convert a path string to a char*
for using some native win32 function ...
You can get byte[] array from string using Encoding.ASCII.GetBytes. This is probably convertible to char* using fixed statement in C#. (This pins the allocated memory, not allowing gc to move it - then you can make a pointer to it).
So my answer is yes only if you manage to convert byte* to char*. (in C/C++ this wouldn't be a problem, but I'm not that sure about C#)
PS> I'll post some code later if I find a bookmark to an article on this. I know I have it somewhere..
You can pass a StringBuilder
as a char*
Have a look at http://pinvoke.net to see if the signature for the function is not already there.
That depends on what you want to do. When you call a Win32 function via PInvoke, you should be able to just pass the String variable; the framework marshals everything for you. If you need something more complicated, have a look at Marshal.StringToHGlobalAnsi and other methods of the Marshal
class.