Click here to Skip to main content
15,908,675 members
Home / Discussions / C#
   

C#

 
GeneralRe: Dataset, Dataview, XML, RadioButtonList Question. HELP!! Pin
Heath Stewart25-Mar-04 14:03
protectorHeath Stewart25-Mar-04 14:03 
GeneralRe: Dataset, Dataview, XML, RadioButtonList Question. HELP!! Pin
bentai29-Mar-04 4:29
bentai29-Mar-04 4:29 
GeneralRe: Dataset, Dataview, XML, RadioButtonList Question. HELP!! Pin
Heath Stewart29-Mar-04 5:14
protectorHeath Stewart29-Mar-04 5:14 
GeneralPassing BStr by refrence to unmanaged dll Pin
schnee2k325-Mar-04 9:45
schnee2k325-Mar-04 9:45 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
Dave Kreskowiak25-Mar-04 12:03
mveDave Kreskowiak25-Mar-04 12:03 
GeneralRe: Passing BStr by refrence to unmanaged dll Pin
schnee2k325-Mar-04 12:10
schnee2k325-Mar-04 12:10 
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 
So, here is what I did:

Importing the function:
<br />
[DllImport("mydll.dll")]<br />
public static extern void MyFunc([MarshalAs(UnmanagedType.BStr)] string A, <br />
   [MarshalAs(UnmanagedType.BStr)] string B,  <br />
   [MarshalAs(UnmanagedType.LPWStr)] ref System.Text.StringBuilder C);<br />


The actual call:
<br />
System.Text.StringBuilder dataOut= new System.Text.StringBuilder(1024);<br />
<br />
MyFunc(someString, anotherString, ref dataOut);<br />


This doesn't work because the pwstr is not the same as an lpwstr. I can verify that the dll is using A and B correctly as BStr's. I get an unhandled exception:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in winp2p.dll

Additional information: Capacity exceeds maximum capacity.


I know for a fact also, that 1024 is WAY more than enough to hold what I need. I think this is happening because, I believe, that a PWSTR is not necasariy null terminated??

Anyways, the following code DOES work, although I HATE that it's in an unsafe block:

Importing the function:
<br />
[DllImport("mydll.dll")]<br />
public static extern void MyFunc([MarshalAs(UnmanagedType.BStr)] string A, <br />
   [MarshalAs(UnmanagedType.BStr)] string B,  <br />
   ref IntPtr C);<br />


The actual call:
<br />
IntPtr dataOut = Marshal.AllocCoTaskMem(1024);<br />
string foo;<br />
<br />
unsafe<br />
{<br />
     MyFunc(someString, anotherString, ref dataOut);<br />
     foo = new string((char*)dataOut.ToPointer());<br />
}<br />
<br />
Marshal.FreeCoTaskMem(dataOut);<br />


In the above example, I do, in fact, get the string I am looking to get, BUT I really really really hate the unsafe block....
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 
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 

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.