|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DllImport Char* and stringI have a (Complied) C Library that needs to be called using c# code, some of the function require char* parameter(in and out) Here is the problem, There is a function that expect an input char* parameter for example int AddString(const char* someStr) And then there is another function to retrieve the value I pass in using the above function for example int GetString(char* rntStr) In C# [DllImport(......)] Int32 AddString(string someStr); [DllImport(..)] Int32 GetString(string rntString); So when I input "aaaa" into someStr and get the return by calling GetString, the rntString variable only return the first character ("a") and the length of the string is 1. Anyone know what's wrong with my code? It the AssString declaration problem or the GetString declaration problem (or both)? thanks Gasnic Yes, I do. You may have to set the Char type in the DllImport to
probably Ansi. Try that. If anyone reads this and gets an example for char** and also for bools in a C++ struct then would like to see an example of that. I think I know how to do it though. Curtis http://www.ghostclip.com The Premier Help System For Developers I have tried to declare the function like this
Int32 AddString([MarshalAs(UnmanagedType.AnsiBStr) string someStr) Int32 GetString([MarshalAs(UnmanagedType.AnsiBStr) ref string rntStr) But I got the same thing. I also tried UnmanagedType.LPStr and UnmanagedType.LPTStr, but both doesn't work. Actually should I use string as the parameter type? thanks Gnic Show quote "Light" <Light65***@gmail.com> wrote in message news:1139275480.103154.90740@f14g2000cwb.googlegroups.com... > Yes, I do. You may have to set the Char type in the DllImport to > probably Ansi. Try that. If anyone reads this and gets an example for > char** and also for bools in a C++ struct then would like to see an > example of that. I think I know how to do it though. > Curtis > http://www.ghostclip.com > The Premier Help System For Developers >
Show quote
"Gnic" <gas***@hotmail.com> wrote in message Mattias already suggested using StringBuilder for your GetString function news:OSiLM37KGHA.516@TK2MSFTNGP15.phx.gbl... >I have tried to declare the function like this > > Int32 AddString([MarshalAs(UnmanagedType.AnsiBStr) string someStr) > > Int32 GetString([MarshalAs(UnmanagedType.AnsiBStr) ref string rntStr) > > But I got the same thing. > > I also tried UnmanagedType.LPStr and UnmanagedType.LPTStr, but both > doesn't work. > > Actually should I use string as the parameter type? since the contents of the buffer look like it needs to be modified. Here's an example of some code that works. Hopefully it will be somewhat applicable to what you want to do: ..CPP file: #include <windows.h> char m_Text[1000]; extern "C" __declspec(dllexport) int AddString(const char* someStr) { strcpy(m_Text, someStr); return 0; } extern "C" __declspec(dllexport) int GetString(char* rntStr) { strcpy(rntStr, m_Text); return 0; } ..CS file: using System.Text; using System.Runtime.InteropServices; class Program { [DllImport("CoolStringLibrary.dll")] static extern int AddString(string someStr); [DllImport("CoolStringLibrary.dll")] static extern int GetString(StringBuilder rntStr); static void Main(string[] args) { AddString("aaaa"); StringBuilder rntStr = new StringBuilder(); GetString(rntStr); } } >[DllImport(..)] For output string parameters you should use StringBuilder as the>Int32 GetString(string rntString); parameter type. Also make sure that if you set the CharSet property in the DllImport attribute, it should be set to Ansi. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message Doesn't C# default to CharSet.Ansi?news:%237tFzE8KGHA.2392@TK2MSFTNGP09.phx.gbl... > >[DllImport(..)] >>Int32 GetString(string rntString); > > For output string parameters you should use StringBuilder as the > parameter type. > > Also make sure that if you set the CharSet property in the DllImport > attribute, it should be set to Ansi. > It does, but since the original poster wrote>Doesn't C# default to CharSet.Ansi? [DllImport(..)] I couldn't know for sure whether or not he or she left the default or specified something else. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. Hello Gnic,
I'm not a C programmer, but I think that's the expected behaviour: Your C library has a return value of char*, not char[]*. Regards. Show quote "Gnic" <gas***@hotmail.com> escribió en el mensaje news:O5Q3mf3KGHA.3496@TK2MSFTNGP14.phx.gbl... | Hi, | | I have a (Complied) C Library that needs to be called using c# code, some of | the function require char* parameter(in and out) | | Here is the problem, | | There is a function that expect an input char* parameter | | for example | int AddString(const char* someStr) | | And then there is another function to retrieve the value I pass in using the | above function | for example | int GetString(char* rntStr) | | In C# | | [DllImport(......)] | Int32 AddString(string someStr); | | [DllImport(..)] | Int32 GetString(string rntString); | | So when I input "aaaa" into someStr and get the return by calling GetString, | the rntString variable only return the first character ("a") and the length | of the string is 1. | | Anyone know what's wrong with my code? | It the AssString declaration problem or the GetString declaration problem | (or both)? | | thanks | | Gasnic Thanks for all the helps, I have the problem fixed by using StringBuilder.
But now I have another problem: The C Library have a struct that contain a unsigned char* field. I read the MSDN PInvoke doc and it said unsigned char should map to byte in ..NET. But what about unsigned char*? should I use byte[] in the class in .NET ( but then I don't know the length in advance and I have to pass the unsigned char* for the c library to fill some data in)? any comment? thanks Show quote "Gnic" <gas***@hotmail.com> wrote in message news:O5Q3mf3KGHA.3496@TK2MSFTNGP14.phx.gbl... > Hi, > > I have a (Complied) C Library that needs to be called using c# code, some > of the function require char* parameter(in and out) > > Here is the problem, > > There is a function that expect an input char* parameter > > for example > int AddString(const char* someStr) > > And then there is another function to retrieve the value I pass in using > the above function > for example > int GetString(char* rntStr) > > In C# > > [DllImport(......)] > Int32 AddString(string someStr); > > [DllImport(..)] > Int32 GetString(string rntString); > > So when I input "aaaa" into someStr and get the return by calling > GetString, the rntString variable only return the first character ("a") > and the length of the string is 1. > > Anyone know what's wrong with my code? > It the AssString declaration problem or the GetString declaration problem > (or both)? > > thanks > > Gasnic > >
Show quote
"Gnic" <gas***@google.ca> wrote in message I think you need to use IntPtr. Marshal.AllocHGlobal(), news:%23csgP0CLGHA.2336@TK2MSFTNGP12.phx.gbl... > Thanks for all the helps, I have the problem fixed by using StringBuilder. > > But now I have another problem: > > The C Library have a struct that contain a unsigned char* field. > I read the MSDN PInvoke doc and it said unsigned char should map to byte > in .NET. > But what about unsigned char*? > > should I use byte[] in the class in .NET ( but then I don't know the > length in advance and I have to pass the unsigned char* for the c library > to fill some data in)? > > any comment? Marshal.FreeHGlobal(), Marshal.ReadByte(), and Marshal.WriteByte() should help you out. |
|||||||||||||||||||||||