Click here to Skip to main content
15,898,035 members
Home / Discussions / C#
   

C#

 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
Dave Kreskowiak25-Mar-04 12:49
mveDave Kreskowiak25-Mar-04 12:49 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
schnee2k325-Mar-04 13:05
schnee2k325-Mar-04 13:05 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
Heath Stewart25-Mar-04 13:46
protectorHeath Stewart25-Mar-04 13:46 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
schnee2k325-Mar-04 14:21
schnee2k325-Mar-04 14:21 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
Dave Kreskowiak25-Mar-04 14:56
mveDave Kreskowiak25-Mar-04 14:56 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
schnee2k325-Mar-04 15:06
schnee2k325-Mar-04 15:06 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
Heath Stewart26-Mar-04 3:06
protectorHeath Stewart26-Mar-04 3:06 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
Heath Stewart26-Mar-04 3:18
protectorHeath Stewart26-Mar-04 3:18 
After reviewing the whole thread, I think I found your problem.

First, a "string is just an array of chars, so char* or char[] (for which a reference references the first element and is typically null-terminated, except for VB which sucks).

So, a PPWSTR is actually a char**, which is not only unnecessary in most cases, but probably incorrect. Also, Dave is right below (man, it feels weird saying that because my CEO who tries to program is named "Dave" and he's always wrong) - BSTRs and LPWSTRs (PWSTR, too) are not the same but can be. Here it's better to assume they won't be and just use UnmanagedType.LPWStr.

So, if your native function is declared as:
void myfunc(PWSTR a, PWSTR b, PWSTR c);
...your P/Invoked declaration should look like:
[DllImport("whatever.dll", CharSet=CharSet.Unicode)]
private static extern void MyFunc(string a, string b, string c);
If one of those is meant to be an [out] param, you can use the OutAttribute but do not use out or ref for a reference type except in rare circumstances where you would have something like PPWSTR.

As I explained above, strings are already reference types. Very rarely do you use ref or out with reference types (interfaces, strings, etc.). There are types, though. QueryInterface, for example, uses LPVOID*, which is void**, so that's one place you'd need it (although there's no reason since the Marshal class already encapsulates this - but it's just an example off the top of my head).

Hope this helps.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
schnee2k326-Mar-04 4:27
schnee2k326-Mar-04 4:27 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
Heath Stewart26-Mar-04 4:31
protectorHeath Stewart26-Mar-04 4:31 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
schnee2k326-Mar-04 4:35
schnee2k326-Mar-04 4:35 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
Dave Kreskowiak26-Mar-04 7:44
mveDave Kreskowiak26-Mar-04 7:44 
Questiontranslation vb to c# ? Pin
el69os25-Mar-04 8:59
el69os25-Mar-04 8:59 
AnswerRe: translation vb to c# ? Pin
Daniel Turini25-Mar-04 9:09
Daniel Turini25-Mar-04 9:09 
AnswerRe: translation vb to c# ? Pin
Michael Flanakin25-Mar-04 11:14
Michael Flanakin25-Mar-04 11:14 
GeneralRe: translation vb to c# ? Pin
el69os26-Mar-04 1:55
el69os26-Mar-04 1:55 
GeneralRe: translation vb to c# ? Pin
Michael Flanakin26-Mar-04 2:06
Michael Flanakin26-Mar-04 2:06 
AnswerRe: translation vb to c# ? Pin
Jeremy Kimball25-Mar-04 14:04
Jeremy Kimball25-Mar-04 14:04 
GeneralRe: translation vb to c# ? Pin
Michael Flanakin25-Mar-04 18:15
Michael Flanakin25-Mar-04 18:15 
GeneralRe: translation vb to c# ? Pin
Daniel Turini26-Mar-04 0:56
Daniel Turini26-Mar-04 0:56 
GeneralRe: translation vb to c# ? Pin
el69os26-Mar-04 2:01
el69os26-Mar-04 2:01 
AnswerRe: translation vb to c# ? Pin
el69os26-Mar-04 1:57
el69os26-Mar-04 1:57 
Generalnewer version Pin
el69os26-Mar-04 2:05
el69os26-Mar-04 2:05 
GeneralAbout DirectX9 and C# Pin
cemlouis25-Mar-04 8:59
cemlouis25-Mar-04 8:59 
GeneralRe: About DirectX9 and C# Pin
Heath Stewart25-Mar-04 9:19
protectorHeath Stewart25-Mar-04 9:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.