Click here to Skip to main content
15,902,635 members
Home / Discussions / C#
   

C#

 
QuestionThreads and BeginReceive Pin
Echilon5-Mar-08 3:28
Echilon5-Mar-08 3:28 
GeneralRepost ( new member ) Pin
led mike5-Mar-08 4:52
led mike5-Mar-08 4:52 
GeneralWebcontrol inside a webcontrol Pin
ezazazel5-Mar-08 3:26
ezazazel5-Mar-08 3:26 
GeneralRe: Webcontrol inside a webcontrol Pin
Vasudevan Deepak Kumar5-Mar-08 4:34
Vasudevan Deepak Kumar5-Mar-08 4:34 
GeneralRe: Webcontrol inside a webcontrol Pin
ezazazel5-Mar-08 5:01
ezazazel5-Mar-08 5:01 
GeneralRe: Webcontrol inside a webcontrol Pin
ezazazel5-Mar-08 6:28
ezazazel5-Mar-08 6:28 
GeneralDataGridView & DataSet - Updating the DataBase Pin
half-life5-Mar-08 3:23
half-life5-Mar-08 3:23 
QuestionP/Invoking void * - is this the right way? Pin
Luca Leonardo Scorcia5-Mar-08 2:54
professionalLuca Leonardo Scorcia5-Mar-08 2:54 
Hello everybody! I have this API published in a C DLL:

int readValue(int objectId, int outputTypeId, void * buf);

Where buf is a pointer to a buffer in memory that can be pretty much anything, depending on the outputTypeId value. In the simplest cases, it points to an int or to a float.

The managed declaration I'm using is:

[DllImport(ReferenceDLL)]<br />
public static extern int H5Aread(int attr_id, int mem_type_id, IntPtr buf);


I have googled a lot and came out with the following solution to get the right value out of that call:

public int GetValueInt32()
{
  // Allocate memory
  int value = 0;
  GCHandle handleValue = GCHandle.Alloc(value, GCHandleType.Pinned);

  // Run the call
  DllImports.H5Aread(ObjectId, ImportedDataTypes.H5T_NATIVE_INT, handleValue.AddrOfPinnedObject());
  value = (int)handleValue.Target;

  handleValue.Free();

  return value;
}

public float GetValueFloat()
{
  // Allocate memory
  float value = 0;
  GCHandle handleValue = GCHandle.Alloc(value, GCHandleType.Pinned);

  // Run the call
  DllImports.H5Aread(ObjectId, ImportedDataTypes.H5T_NATIVE_FLOAT, handleValue.AddrOfPinnedObject());
  value = (float) handleValue.Target;

  handleValue.Free();

  return value;
}


It works, but I'm a bit puzzled. According to my understanding of how GCHandle should work, it shouldn't be necessary to copy the value of handleValue.Target into value - it's just pinning the address of value to avoid moving. But if omit the copying step, value is always zero.
If this is the way GCHandle is supposed to work, then why I'm passing an instance of value to GCHandle.Alloc instead of the memory size to allocate?

If anyone can shed more light on this topic, I'd be really grateful. My next project is going to use a lot of P/Invokes, and I really need to understand what's going on there.

Is there's a "right" way to accomplish what I'm trying to do?

Luca

The Price of Freedom is Eternal Vigilance. -- Wing Commander IV

En Það Besta Sem Guð Hefur Skapað, Er Nýr Dagur.
(But the best thing God has created, is a New Day.)
-- Sigur Ròs - Viðrar vel til loftárása

AnswerRe: P/Invoking void * - is this the right way? Pin
led mike5-Mar-08 4:56
led mike5-Mar-08 4:56 
AnswerRe: P/Invoking void * - is this the right way? Pin
Luc Pattyn5-Mar-08 5:43
sitebuilderLuc Pattyn5-Mar-08 5:43 
GeneralRe: P/Invoking void * - is this the right way? Pin
Luca Leonardo Scorcia5-Mar-08 7:32
professionalLuca Leonardo Scorcia5-Mar-08 7:32 
AnswerRe: P/Invoking void * - is this the right way? Pin
Ernest Laurentin5-Mar-08 5:59
Ernest Laurentin5-Mar-08 5:59 
GeneralInvalidPrinterException when printer is installed and working - only with HP LaserJet 1010 series Pin
Dragan Matic5-Mar-08 2:22
Dragan Matic5-Mar-08 2:22 
GeneralStarting point to Crystal Reports Pin
Harvey Saayman5-Mar-08 2:16
Harvey Saayman5-Mar-08 2:16 
General[Message Deleted] Pin
Krishnaraj Barvathaya B5-Mar-08 0:52
Krishnaraj Barvathaya B5-Mar-08 0:52 
GeneralRe: How to copy files in a folder tree into a single folder Pin
pmarfleet5-Mar-08 1:26
pmarfleet5-Mar-08 1:26 
GeneralRe: How to copy files in a folder tree into a single folder Pin
Krishnaraj Barvathaya B5-Mar-08 5:48
Krishnaraj Barvathaya B5-Mar-08 5:48 
GeneralRe: How to copy files in a folder tree into a single folder Pin
Anthony Mushrow5-Mar-08 6:03
professionalAnthony Mushrow5-Mar-08 6:03 
GeneralRe: How to copy files in a folder tree into a single folder Pin
pmarfleet5-Mar-08 8:32
pmarfleet5-Mar-08 8:32 
GeneralRe: How to copy files in a folder tree into a single folder Pin
Pete O'Hanlon5-Mar-08 1:32
mvePete O'Hanlon5-Mar-08 1:32 
GeneralUnable to access SQL Reports in C#.Net windows application Pin
Vinay Babu4-Mar-08 23:20
Vinay Babu4-Mar-08 23:20 
GeneralRe: Unable to access SQL Reports in C#.Net windows application Pin
pmarfleet5-Mar-08 1:27
pmarfleet5-Mar-08 1:27 
GeneralRe: Unable to access SQL Reports in C#.Net windows application Pin
Paul Conrad5-Mar-08 4:32
professionalPaul Conrad5-Mar-08 4:32 
QuestionHow do I write to another window through a C# form [modified] Pin
Deresen4-Mar-08 23:07
Deresen4-Mar-08 23:07 
AnswerRe: How do I write to another window through a C# form Pin
V.4-Mar-08 23:48
professionalV.4-Mar-08 23:48 

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.