marshaling wchar_t* as out parameter

C

c_xyTopa

hi all!
i have a c++ function, which becomes wchar_t* as in-parameter:

void func1(wchar_t *name){}

the coresponding method in c# is:
[DllImport("test.dll")]
static extern void func1(string name);

that works fine.

but if the wchar_t *name is out-parameter, same method declaration in c#
doesn't work. what do i do wrong?
 
C

c_xyTopa

i failed with StringBuilder too. may be it has smth to do with "const"
key-word?

extern "C" __declspec(dllexport) void func(int index, const wchar_t
*name, int *x, int *y)
{
name = SearchResults[index].name;
*x = SearchResults[index].pos.x;
*y = SearchResults[index].pos.y;
}

[DllImport("test.dll")]
static extern void func(int index, StringBuilder name, ref int x, ref
int y);

You you should use the StringBuilder for this.

--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:

hi all!
i have a c++ function, which becomes wchar_t* as in-parameter:

void func1(wchar_t *name){}

the coresponding method in c# is:
[DllImport("test.dll")]
static extern void func1(string name);

that works fine.

but if the wchar_t *name is out-parameter, same method declaration in c#
doesn't work. what do i do wrong?
 
C

c_xyTopa

if i use wcscpy(name, SearchResults[index].name); it works.
so why can't a reference just be reassigned?


c_xyTopa said:
i failed with StringBuilder too. may be it has smth to do with "const"
key-word?

extern "C" __declspec(dllexport) void func(int index, const wchar_t
*name, int *x, int *y)
{
name = SearchResults[index].name;
*x = SearchResults[index].pos.x;
*y = SearchResults[index].pos.y;
}

[DllImport("test.dll")]
static extern void func(int index, StringBuilder name, ref int x, ref
int y);

You you should use the StringBuilder for this.

--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:

hi all!
i have a c++ function, which becomes wchar_t* as in-parameter:

void func1(wchar_t *name){}

the coresponding method in c# is:
[DllImport("test.dll")]
static extern void func1(string name);

that works fine.

but if the wchar_t *name is out-parameter, same method declaration in
c# doesn't work. what do i do wrong?
 
S

Sergey Bogdanov

The "wchar *" is just a pointer to a string. When you write:

name = SearchResults[index].name

you copy only a pointer. That is why if you want to copy the string
which pointed by this pointer you have to use wstrcpy.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


c_xyTopa said:
if i use wcscpy(name, SearchResults[index].name); it works.
so why can't a reference just be reassigned?


c_xyTopa said:
i failed with StringBuilder too. may be it has smth to do with "const"
key-word?

extern "C" __declspec(dllexport) void func(int index, const wchar_t
*name, int *x, int *y)
{
name = SearchResults[index].name;
*x = SearchResults[index].pos.x;
*y = SearchResults[index].pos.y;
}

[DllImport("test.dll")]
static extern void func(int index, StringBuilder name, ref int x, ref
int y);

You you should use the StringBuilder for this.

--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:


hi all!
i have a c++ function, which becomes wchar_t* as in-parameter:

void func1(wchar_t *name){}

the coresponding method in c# is:
[DllImport("test.dll")]
static extern void func1(string name);

that works fine.

but if the wchar_t *name is out-parameter, same method declaration
in c# doesn't work. what do i do wrong?
 
C

c_xyTopa

i understand that.

my question is why can not a pointer just be copied? why should i copy
the string itself?

thanx in advance


Sergey said:
The "wchar *" is just a pointer to a string. When you write:

name = SearchResults[index].name

you copy only a pointer. That is why if you want to copy the string
which pointed by this pointer you have to use wstrcpy.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


c_xyTopa said:
if i use wcscpy(name, SearchResults[index].name); it works.
so why can't a reference just be reassigned?


c_xyTopa said:
i failed with StringBuilder too. may be it has smth to do with
"const" key-word?

extern "C" __declspec(dllexport) void func(int index, const wchar_t
*name, int *x, int *y)
{
name = SearchResults[index].name;
*x = SearchResults[index].pos.x;
*y = SearchResults[index].pos.y;
}

[DllImport("test.dll")]
static extern void func(int index, StringBuilder name, ref int x, ref
int y);


Alex Yakhnin [MVP] schrieb:

You you should use the StringBuilder for this.

--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:


hi all!
i have a c++ function, which becomes wchar_t* as in-parameter:

void func1(wchar_t *name){}

the coresponding method in c# is:
[DllImport("test.dll")]
static extern void func1(string name);

that works fine.

but if the wchar_t *name is out-parameter, same method declaration
in c# doesn't work. what do i do wrong?
 
S

Sergey Bogdanov

You have to consider pointer to pointer (wchar_t **, and use *name =
SearchResults[index].name) for such operations but I'm not sure that it
will work for you for CF application. That is why the best solution in
you case would be to use wstrcpy.


--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


c_xyTopa said:
i understand that.

my question is why can not a pointer just be copied? why should i copy
the string itself?

thanx in advance


Sergey said:
The "wchar *" is just a pointer to a string. When you write:

name = SearchResults[index].name

you copy only a pointer. That is why if you want to copy the string
which pointed by this pointer you have to use wstrcpy.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


c_xyTopa said:
if i use wcscpy(name, SearchResults[index].name); it works.
so why can't a reference just be reassigned?


c_xyTopa schrieb:

i failed with StringBuilder too. may be it has smth to do with
"const" key-word?

extern "C" __declspec(dllexport) void func(int index, const wchar_t
*name, int *x, int *y)
{
name = SearchResults[index].name;
*x = SearchResults[index].pos.x;
*y = SearchResults[index].pos.y;
}

[DllImport("test.dll")]
static extern void func(int index, StringBuilder name, ref int x,
ref int y);


Alex Yakhnin [MVP] schrieb:

You you should use the StringBuilder for this.

--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com | www.opennetcf.org


:


hi all!
i have a c++ function, which becomes wchar_t* as in-parameter:

void func1(wchar_t *name){}

the coresponding method in c# is:
[DllImport("test.dll")]
static extern void func1(string name);

that works fine.

but if the wchar_t *name is out-parameter, same method declaration
in c# doesn't work. what do i do wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top