#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    3
    Rep Power
    0

    Question Calling unmanaged C function with void* parameter from managed C#


    Hi,

    I have the following unmanaged c-function that I'm trying
    to call from C#:

    getfieldvalue(char *field, void *fieldptr)

    Function should query the value of the given field (char
    *field) and store it to the second argument (void
    *fieldptr).

    I found an example from MSDN library how to declare
    unmanaged function that gets void* paramater as one of its
    arguments like this:

    [ DllImport( "my.dll" )]
    public static extern void get_fieldvalue(string field,
    [MarshalAs(UnmanagedType.AsAny)] Object o );

    But it has one problem. When I pass to function for
    example object of type int it can't change the value of
    the variable because it's passed by value.

    The managed calling C# method looks like this:

    public static int GetFieldValue()
    {
    int val = 1511;

    getfieldvalue("myfield", val);
    MessageBox.Show("Value after call: " + val);
    // (The value is still 1511)

    return 42;
    }

    Is there some elegant way to declare the function with
    [DllImport] so that it could also change the value of the
    argument of type void*?

    (I know I could use overloading and declare different
    [DllImport] statements for every different argument type,
    but I would prefer one more generic declaration, that
    would work for both value and reference types as MSDN
    example above.)

    Your help would be greatly appreciated.

    Mikko
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    Try an unsafe block:
    Code:
    public unsafe static int GetFieldValue()
    {
    	int val = 1511;
    
    	getfieldvalue("myfield", &val);
    	MessageBox.Show("Value after call: " + val);
    
    	return 42;
    }
    or passing by ref
    Code:
    [ DllImport( "my.dll" )] 
    public static extern void get_fieldvalue(string field, 
    [MarshalAs(UnmanagedType.AsAny)] ref Object o );
    
    public static int GetFieldValue()
    {
    	int val = 1511;
    
    	getfieldvalue("myfield", ref val);
    	MessageBox.Show("Value after call: " + val);
    
    	return 42;
    }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    3
    Rep Power
    0
    Thanks for your advice,

    but when I'm trying to compile following code:

    [ DllImport( "my.dll" )]
    public static extern void get_fieldvalue(string field,
    [MarshalAs(UnmanagedType.AsAny)] ref Object o );

    public static int GetFieldValue()
    {
    int val = 1511;

    getfieldvalue("myfield", ref val);
    MessageBox.Show("Value after call: " + val); // (row 206)

    return 42;
    }

    I get the following error message:

    C:\...\testsharp.cs(206): Argument '2': cannot convert from 'ref int' to 'ref object'

    Otherwise this would be exactly what a want. Is there any way how I could pass both value types (like int, float...) and reference types to method using just one function declaration for c-function? Or maybe at least one function declaration for all the valuetypes?

    Mikko
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    Does ref (Object)val work?
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    3
    Rep Power
    0
    No it doesn't and it seems that function overloading or giving IntPtr parameter instead of object are the only possibilities to solve this problem...

    http://communities.microsoft.com/new...6.phx.gbl%253E

IMN logo majestic logo threadwatch logo seochat tools logo