I am attempting to write a .NET wrapper for a C++ DLL file, but am having
problems with passing strings as parameters. How should I be writing my C#
function call when the C header file is definined as taking a char * as an
argument? For example the C++ header says
SDCERR GetCurrentConfig(DWORD *num, char *name);
I am using Uint for the *num parameter, which returns the correct value
but for *name, I always get back a string of 6 squares. I have tried using
string, StringBuilder and char arrays, and passing by ref and not, but
always get the same result.
Connected to this I am also having problems with passing structures for
C++ functions - the structures contain strings as well. Whenever I call a
function that requires a structure as a parameter, I always get a
NotSupportedException, does this just mean that I have defined my structure
incorrectly? Do I have to initailise the structure beforehand (have tried
this but still didn't work)?
I am using Visual Studio 2005, and if it makes a difference it is for a
Compact Framework device. Thanks in advance for any help.
Andy Baker 6 3697
Have you tried passing a StringBuilder?
Yes, I tried StringBuilder. From what I read on the net, that's what I
thought I should be doing. The code I am using is as follows:-
uint Number = 0;
StringBuilder Name = new StringBuilder(CONFIG_NAME_SZ);
SDCERR Result = GetCurrentConfig(ref Number, Name);
Result contains a valid value, as does Number, but Name just contains
rubbish. If I pass Name with the ref keyword I get a NotSupportedException.
To convert the StringBuilder to a string, I am just using ToString - is this
correct?
Andy Baker
<pu****@googlemail.comwrote in message
news:5b**********************************@f63g2000 hsf.googlegroups.com...
Have you tried passing a StringBuilder?
On Jul 2, 3:17*pm, "Andy Baker" <aba...@NOSPAMvanputer.comwrote:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having
problems with passing strings as parameters. How should I be writing my C#
function call when the C header file is definined as taking a char * as an
argument? For example the C++ header says
* * SDCERR GetCurrentConfig(DWORD *num, char *name);
* * I am using Uint for the *num parameter, which returns the correct value
but for *name, I always get back a string of 6 squares. I have tried using
string, StringBuilder and char arrays, and passing by ref and not, but
always get the same result.
* * Connected to this I am also having problems with passing structures for
C++ functions - the structures contain strings as well. Whenever I call a
function that requires a structure as a parameter, I always get a
NotSupportedException, does this just mean that I have defined my structure
incorrectly? Do I have to initailise the structure beforehand (have tried
this but still didn't work)?
* * I am using Visual Studio 2005, and if it makes a difference it is for a
Compact Framework device. Thanks in advance for any help.
Can you please show your P/Invoke declaration for this function?
Perhaps you've forgotten CharSet?
Try this also:
[DllImport(CharSet = CharSet.Ansi)]
SDCERR GetCurrentConfig(ref uint num, StringBuilder name);
Andy, a couple options:
1) Look at the MarshalAsAttribute which tells .NET how to translate its
unicode string between it and unmanaged code; the attribute is applied to the
argument, here's the example from MSDN:
void MyMethod( [MarshalAs(LPStr)] string s);
2) The other option is to write the wrapper in managed C++; if you go this
route your method calls will look something like this:
void MyMethod(String^ s)
{
IntPtr ptr = Marshal::StringToHGlobalAnsi(str);;
try
{
SDCERR err = GetCurrentConfig( (char*)ptr.ToPointer() );
}
finally
{
if (ptr.ToPointer()) Marshal::FreeHGlobal(ptr);
}
}
HTH - KH
"Andy Baker" wrote:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having
problems with passing strings as parameters. How should I be writing my C#
function call when the C header file is definined as taking a char * as an
argument? For example the C++ header says
SDCERR GetCurrentConfig(DWORD *num, char *name);
I am using Uint for the *num parameter, which returns the correct value
but for *name, I always get back a string of 6 squares. I have tried using
string, StringBuilder and char arrays, and passing by ref and not, but
always get the same result.
Connected to this I am also having problems with passing structures for
C++ functions - the structures contain strings as well. Whenever I call a
function that requires a structure as a parameter, I always get a
NotSupportedException, does this just mean that I have defined my structure
incorrectly? Do I have to initailise the structure beforehand (have tried
this but still didn't work)?
I am using Visual Studio 2005, and if it makes a difference it is for a
Compact Framework device. Thanks in advance for any help.
Andy Baker
My P/Invoke declaration is as follows:
[DllImport("VPSummit.dll", CharSet = CharSet.Unicode"]
public static extern SDCERR GetCurrentConfig(ref unit num, StringBuilder
name);
I have also tried CharSet.Auto - CharSet.Ansi is not a option for the
Compact Framework (I think)
"Pavel Minaev" <in****@gmail.comwrote in message
news:1f**********************************@w7g2000h sa.googlegroups.com...
On Jul 2, 3:17 pm, "Andy Baker" <aba...@NOSPAMvanputer.comwrote:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having
problems with passing strings as parameters. How should I be writing my C#
function call when the C header file is definined as taking a char * as an
argument? For example the C++ header says
SDCERR GetCurrentConfig(DWORD *num, char *name);
I am using Uint for the *num parameter, which returns the correct value
but for *name, I always get back a string of 6 squares. I have tried using
string, StringBuilder and char arrays, and passing by ref and not, but
always get the same result.
Connected to this I am also having problems with passing structures for
C++ functions - the structures contain strings as well. Whenever I call a
function that requires a structure as a parameter, I always get a
NotSupportedException, does this just mean that I have defined my
structure
incorrectly? Do I have to initailise the structure beforehand (have tried
this but still didn't work)?
I am using Visual Studio 2005, and if it makes a difference it is for a
Compact Framework device. Thanks in advance for any help.
Can you please show your P/Invoke declaration for this function?
Perhaps you've forgotten CharSet?
Try this also:
[DllImport(CharSet = CharSet.Ansi)]
SDCERR GetCurrentConfig(ref uint num, StringBuilder name);
Thanks for the advice. I have been looking at the MarshalAs attribute in
relation to passing structures, but hadn't tried it in the DllImport
declaration. However, I am obviously doing something wrong as every time I
try it I am getting a NotSupportedException.
As I understand even less about C++ that I do about C# (which isn't
alot!) I don't think the managed C++ route is the one to go down. I think I
need someone more experienced to do this job for me! Thanks again.
Andy Baker
"KH" <KH@discussions.microsoft.comwrote in message
news:9C**********************************@microsof t.com...
Andy, a couple options:
1) Look at the MarshalAsAttribute which tells .NET how to translate its
unicode string between it and unmanaged code; the attribute is applied to
the
argument, here's the example from MSDN:
void MyMethod( [MarshalAs(LPStr)] string s);
2) The other option is to write the wrapper in managed C++; if you go this
route your method calls will look something like this:
void MyMethod(String^ s)
{
IntPtr ptr = Marshal::StringToHGlobalAnsi(str);;
try
{
SDCERR err = GetCurrentConfig( (char*)ptr.ToPointer() );
}
finally
{
if (ptr.ToPointer()) Marshal::FreeHGlobal(ptr);
}
}
HTH - KH
"Andy Baker" wrote:
>I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is definined as taking a char * as an argument? For example the C++ header says SDCERR GetCurrentConfig(DWORD *num, char *name); I am using Uint for the *num parameter, which returns the correct value but for *name, I always get back a string of 6 squares. I have tried using string, StringBuilder and char arrays, and passing by ref and not, but always get the same result. Connected to this I am also having problems with passing structures for C++ functions - the structures contain strings as well. Whenever I call a function that requires a structure as a parameter, I always get a NotSupportedException, does this just mean that I have defined my structure incorrectly? Do I have to initailise the structure beforehand (have tried this but still didn't work)? I am using Visual Studio 2005, and if it makes a difference it is for a Compact Framework device. Thanks in advance for any help.
Andy Baker This discussion thread is closed Replies have been disabled for this discussion. Similar topics
5 posts
views
Thread by harry |
last post: by
|
2 posts
views
Thread by muser |
last post: by
|
4 posts
views
Thread by Suddn |
last post: by
|
5 posts
views
Thread by Jack |
last post: by
|
39 posts
views
Thread by Mike MacSween |
last post: by
|
3 posts
views
Thread by Mark |
last post: by
|
reply
views
Thread by Dave Cullen |
last post: by
|
61 posts
views
Thread by academic |
last post: by
|
4 posts
views
Thread by arnuld |
last post: by
| | | | | | | | | | |