Click here to Skip to main content
15,925,255 members
Home / Discussions / C#
   

C#

 
GeneralAutomatically select first row of datagrid Pin
Code Toad17-Mar-04 9:09
Code Toad17-Mar-04 9:09 
GeneralRe: Automatically select first row of datagrid Pin
Mazdak17-Mar-04 10:28
Mazdak17-Mar-04 10:28 
GeneralMarshaling Arrays Pin
Tristan Rhodes17-Mar-04 7:37
Tristan Rhodes17-Mar-04 7:37 
GeneralRe: Marshaling Arrays Pin
Jeremy Kimball17-Mar-04 8:02
Jeremy Kimball17-Mar-04 8:02 
GeneralRe: Marshaling Arrays Pin
Tristan Rhodes17-Mar-04 8:10
Tristan Rhodes17-Mar-04 8:10 
GeneralRe: Marshaling Arrays Pin
Jeremy Kimball17-Mar-04 8:22
Jeremy Kimball17-Mar-04 8:22 
GeneralRe: Marshaling Arrays Pin
Tristan Rhodes17-Mar-04 8:34
Tristan Rhodes17-Mar-04 8:34 
GeneralRe: Marshaling Arrays Pin
Heath Stewart17-Mar-04 8:51
protectorHeath Stewart17-Mar-04 8:51 
Putting data in a SAFEARRAY isn't always an option - you can't be sure what's in an IStream unless you're explicitly putting it there.

Using the correctly marshalling attributes is the first step. Again, keep in mind that a pointer to an array is just the address of the first element. If you look at ALL the native APIs that use arrays (even those that use LPTSTR, which is nothing more than array of TCHARs), they always require an [in] or [in/out] specifying how many elements to expect. This is because you get the address of the first element and you keep offsetting the address by the bit width of the processor (typically) X number of times where X is the number of elements.

Also keep in mind that an array is already a reference type (even if it is an array of value types), so including the out or ref keywords is often unnecessary. Pass an instance of the array. Take, for instance, the GetComputerName API (off the top of my head):
BOOL GetComputerName(/*[out]*/ LPTSTR lpBuffer, /*[in,out]*/ LPDWORD lpnSize);
In .NET, you'd simply declare and use it like:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern bool GetComputerName(string buffer,
  [MarshalAs(UnmanagedType.U4)] ref int size);
//...
int size = 16;
string buffer = new string(size, '\0'); // Initialize to null chars.
GetComputerName(buffer, ref size);
The same is true for arrays.

The problem is once you get your array of bytes (don't ever assume their characters), now you must get it into a managed object or read/write it as is.

 

Microsoft MVP, Visual C#
My Articles
GeneralRe: Marshaling Arrays Pin
Tristan Rhodes17-Mar-04 9:20
Tristan Rhodes17-Mar-04 9:20 
GeneralRe: Marshaling Arrays Pin
Heath Stewart18-Mar-04 3:37
protectorHeath Stewart18-Mar-04 3:37 
GeneralRe: Marshaling Arrays Pin
Jeremy Kimball17-Mar-04 9:45
Jeremy Kimball17-Mar-04 9:45 
GeneralRe: Marshaling Arrays Pin
Tristan Rhodes17-Mar-04 9:57
Tristan Rhodes17-Mar-04 9:57 
GeneralRe: Marshaling Arrays Pin
Jeremy Kimball17-Mar-04 9:59
Jeremy Kimball17-Mar-04 9:59 
GeneralRe: Marshaling Arrays Pin
Heath Stewart17-Mar-04 13:10
protectorHeath Stewart17-Mar-04 13:10 
GeneralLame! Pin
Tristan Rhodes17-Mar-04 10:15
Tristan Rhodes17-Mar-04 10:15 
GeneralRe: Lame! Pin
Jeremy Kimball17-Mar-04 10:46
Jeremy Kimball17-Mar-04 10:46 
GeneralRe: Lame! Pin
Tristan Rhodes17-Mar-04 10:59
Tristan Rhodes17-Mar-04 10:59 
GeneralRe: Lame! Pin
Jeremy Kimball17-Mar-04 11:12
Jeremy Kimball17-Mar-04 11:12 
GeneralRe: Lame! Pin
Tristan Rhodes17-Mar-04 11:41
Tristan Rhodes17-Mar-04 11:41 
GeneralMore quirky help (Incrimented UI Varibles) Pin
MKlucher17-Mar-04 7:09
MKlucher17-Mar-04 7:09 
GeneralRe: More quirky help (Incrimented UI Varibles) Pin
Jeremy Kimball17-Mar-04 7:33
Jeremy Kimball17-Mar-04 7:33 
GeneralRe: More quirky help (Incrimented UI Varibles) Pin
MKlucher17-Mar-04 8:55
MKlucher17-Mar-04 8:55 
GeneralToolbar without images Pin
mcgahanfl17-Mar-04 6:38
mcgahanfl17-Mar-04 6:38 
GeneralRe: Toolbar without images Pin
surgeproof17-Mar-04 7:01
surgeproof17-Mar-04 7:01 
Generalget Addresses from outlook express Pin
Anonymous17-Mar-04 6:19
Anonymous17-Mar-04 6: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.