Bug 6657 - P/Invoke mess when returning a struct (x86 only)
Summary: P/Invoke mess when returning a struct (x86 only)
Status: RESOLVED NORESPONSE
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-08-23 03:15 UTC by Florian Kummer
Modified: 2012-08-24 09:44 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sorce (Native, C#) and output listing and compile script (1.57 KB, application/x-zip-compressed)
2012-08-23 03:15 UTC, Florian Kummer
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED NORESPONSE

Description Florian Kummer 2012-08-23 03:15:57 UTC
Created attachment 2396 [details]
Sorce (Native, C#) and output listing and compile script

Note: seems to be similar to Bug 4678: 'P/Invoke with struct return values crashes on x86', but affects different Platform; I think my example is more elementary.

I have a native library (libfakedl.so), containing the function:

  void* my_dlopen(const char* file, int flag) {
    printf("Filename = %s \n", file);
    printf("flag = %d \n", flag);
      
    return (void*)(666);
  }

I compare two different P/Invoke defintitions; the frist one is just:

  [DllImport("fakedl", EntryPoint="my_dlopen", CharSet = CharSet.Ansi)]
  static extern IntPtr dlopen_working(string filename, int flag);

But for type saefty reasons, I usually declare my own return type:

  [StructLayout(LayoutKind.Sequential)]
  public struct DynLibHandle {
     public IntPtr val;
  }

  [DllImport("fakedl", EntryPoint="my_dlopen", CharSet = CharSet.Ansi)]
  static extern DynLibHandle dlopen_failing(string filename, int flag);

The later declaration works fine on a x64-mono, but not on a x86 mono (a very old machine).
Using the first declaration, everything is fine, the C-Routine prints
exactly the values I have passed, also the return value is correct.
But the second declaration seems to be messy: the C-function prints

Filename = 8�п���ة�     ,�п�b�  @�V�▒ԛ   
flag = 161444176 

and the return value is wrong.

This error occurs only on a x86-machine (on two linux variants, OpenSUSE and Debian), on a x64-Debian-system everything is fine.
Comment 1 Zoltan Varga 2012-08-23 06:03:30 UTC
The second declaration is not correct, the C routine returns a pointer, not a struct containing one pointer. The two might be returned the same way on some architectures, like x86-64, but returned differently on x86.
Comment 2 Florian Kummer 2012-08-24 09:44:59 UTC
Thanks for the quick reply;
I learned somethinng today.