P/Invoke array in struct

  • Thread starter Thread starter Chris Forsberg [MVP]
  • Start date Start date
C

Chris Forsberg [MVP]

My C isn't what it should be, so can anyone translate the following C code
to .NET CF? Many thanks in advance!

struct DefinitionParms {
void * phAdapterParms;
int destParmLen;
};

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

defParams.phAdapterParms = address;
defParams.destParmLen = 2;
 
Chris,

Obviously, void * phAdapterParms would be translated into
IntPtr phAdapterParms. You'd need to use Marshal.Copy to
copy your managed array address[2] to this pointer. You'd
probably need to allocate some memory before doing that...

HTH... Alex
 
Thanks, Alex, so you mean...

struct DefinitionParms {
IntPtr phAdapterParms;
int destParmLen;
}

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

IntPtr addressPtr = Memory.AllocHLocal(8); // bet you can guess what this
does

Marshal.Copy(address, 0, addressPtr, 8);

defParams.phAdapterParms = addressPtr;
defParams.destParmLen = 2;

Memory.FreeHLocal(addressPtr);


Alex Yakhnin said:
Chris,

Obviously, void * phAdapterParms would be translated into
IntPtr phAdapterParms. You'd need to use Marshal.Copy to
copy your managed array address[2] to this pointer. You'd
probably need to allocate some memory before doing that...

HTH... Alex
--
Alex Yakhnin, MVP
IntelliProg, Inc.
http://www.intelliprog.com
-----Original Message-----
My C isn't what it should be, so can anyone translate the following C code
to .NET CF? Many thanks in advance!

struct DefinitionParms {
void * phAdapterParms;
int destParmLen;
};

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

defParams.phAdapterParms = address;
defParams.destParmLen = 2;


.
 
It won't be that easy. You also need to make sure that
the target objects are also in the unmanaged memory.

I've been dealing with a similar problem when marshalling
IP_ADAPTER_INFO and other IPHelper routines. You can take
a look at http://www.alexfeinman.com/download.asp?
doc=GetAdapterInfo.zip for an example of that approach
-----Original Message-----
Thanks, Alex, so you mean...

struct DefinitionParms {
IntPtr phAdapterParms;
int destParmLen;
}

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

IntPtr addressPtr = Memory.AllocHLocal(8); // bet you can guess what this
does

Marshal.Copy(address, 0, addressPtr, 8);

defParams.phAdapterParms = addressPtr;
defParams.destParmLen = 2;

Memory.FreeHLocal(addressPtr);


Chris,

Obviously, void * phAdapterParms would be translated into
IntPtr phAdapterParms. You'd need to use Marshal.Copy to
copy your managed array address[2] to this pointer. You'd
probably need to allocate some memory before doing that...

HTH... Alex
--
Alex Yakhnin, MVP
IntelliProg, Inc.
http://www.intelliprog.com
-----Original Message-----
My C isn't what it should be, so can anyone translate
the
following C code
to .NET CF? Many thanks in advance!

struct DefinitionParms {
void * phAdapterParms;
int destParmLen;
};

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

defParams.phAdapterParms = address;
defParams.destParmLen = 2;


.


.
 
I understand, and I would really like to see your sample I couldn't download
(and yes, I have merged the two lines of the link ;-)...

Alex Feinman said:
It won't be that easy. You also need to make sure that
the target objects are also in the unmanaged memory.

I've been dealing with a similar problem when marshalling
IP_ADAPTER_INFO and other IPHelper routines. You can take
a look at http://www.alexfeinman.com/download.asp?
doc=GetAdapterInfo.zip for an example of that approach
-----Original Message-----
Thanks, Alex, so you mean...

struct DefinitionParms {
IntPtr phAdapterParms;
int destParmLen;
}

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

IntPtr addressPtr = Memory.AllocHLocal(8); // bet you can guess what this
does

Marshal.Copy(address, 0, addressPtr, 8);

defParams.phAdapterParms = addressPtr;
defParams.destParmLen = 2;

Memory.FreeHLocal(addressPtr);


Chris,

Obviously, void * phAdapterParms would be translated into
IntPtr phAdapterParms. You'd need to use Marshal.Copy to
copy your managed array address[2] to this pointer. You'd
probably need to allocate some memory before doing that...

HTH... Alex
--
Alex Yakhnin, MVP
IntelliProg, Inc.
http://www.intelliprog.com

-----Original Message-----
My C isn't what it should be, so can anyone translate the
following C code
to .NET CF? Many thanks in advance!

struct DefinitionParms {
void * phAdapterParms;
int destParmLen;
};

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

defParams.phAdapterParms = address;
defParams.destParmLen = 2;


.


.
 
My bad. It's http://www.alexfeinman.com/download.asp?doc=AdapterInfo.zip
BTW the url was wrapped because I had to resort to using WebNews - the news
server was having another round of problems


Chris Forsberg said:
I understand, and I would really like to see your sample I couldn't download
(and yes, I have merged the two lines of the link ;-)...

Alex Feinman said:
It won't be that easy. You also need to make sure that
the target objects are also in the unmanaged memory.

I've been dealing with a similar problem when marshalling
IP_ADAPTER_INFO and other IPHelper routines. You can take
a look at http://www.alexfeinman.com/download.asp?
doc=GetAdapterInfo.zip for an example of that approach
-----Original Message-----
Thanks, Alex, so you mean...

struct DefinitionParms {
IntPtr phAdapterParms;
int destParmLen;
}

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

IntPtr addressPtr = Memory.AllocHLocal(8); // bet you can guess what this
does

Marshal.Copy(address, 0, addressPtr, 8);

defParams.phAdapterParms = addressPtr;
defParams.destParmLen = 2;

Memory.FreeHLocal(addressPtr);


Chris,

Obviously, void * phAdapterParms would be translated into
IntPtr phAdapterParms. You'd need to use Marshal.Copy to
copy your managed array address[2] to this pointer. You'd
probably need to allocate some memory before doing that...

HTH... Alex
--
Alex Yakhnin, MVP
IntelliProg, Inc.
http://www.intelliprog.com

-----Original Message-----
My C isn't what it should be, so can anyone translate the
following C code
to .NET CF? Many thanks in advance!

struct DefinitionParms {
void * phAdapterParms;
int destParmLen;
};

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

defParams.phAdapterParms = address;
defParams.destParmLen = 2;


.



.
 
Thanks, Alex!

Alex Feinman said:
My bad. It's http://www.alexfeinman.com/download.asp?doc=AdapterInfo.zip
BTW the url was wrapped because I had to resort to using WebNews - the news
server was having another round of problems


Chris Forsberg said:
I understand, and I would really like to see your sample I couldn't download
(and yes, I have merged the two lines of the link ;-)...

Alex Feinman said:
It won't be that easy. You also need to make sure that
the target objects are also in the unmanaged memory.

I've been dealing with a similar problem when marshalling
IP_ADAPTER_INFO and other IPHelper routines. You can take
a look at http://www.alexfeinman.com/download.asp?
doc=GetAdapterInfo.zip for an example of that approach

-----Original Message-----
Thanks, Alex, so you mean...

struct DefinitionParms {
IntPtr phAdapterParms;
int destParmLen;
}

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

IntPtr addressPtr = Memory.AllocHLocal(8); // bet you
can guess what this
does

Marshal.Copy(address, 0, addressPtr, 8);

defParams.phAdapterParms = addressPtr;
defParams.destParmLen = 2;

Memory.FreeHLocal(addressPtr);


in message
Chris,

Obviously, void * phAdapterParms would be translated
into
IntPtr phAdapterParms. You'd need to use Marshal.Copy
to
copy your managed array address[2] to this pointer.
You'd
probably need to allocate some memory before doing
that...

HTH... Alex
--
Alex Yakhnin, MVP
IntelliProg, Inc.
http://www.intelliprog.com

-----Original Message-----
My C isn't what it should be, so can anyone translate
the
following C code
to .NET CF? Many thanks in advance!

struct DefinitionParms {
void * phAdapterParms;
int destParmLen;
};

DefinitionParms defParams;

int first = 1;
int second = 2;

int address[2] = { first, second };

defParams.phAdapterParms = address;
defParams.destParmLen = 2;


.



.
 
Back
Top