question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Export function with out parameter

See original GitHub issue

Hi, I’m trying to migrate code that previously used DllExport so it could be used by InnoSetup.

Previously I had

[MarshalAs(UnmanagedType.LPWStr)] string input, [MarshalAs(UnmanagedType.BStr)] out string output

I can’t make an out IntPtr though

  [UnmanagedCallersOnly(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvStdcall) })]
        public static void ReturnString(IntPtr value, out IntPtr result)
        {
            var message = Marshal.PtrToStringUni(value);
            _ = NativeMethods.MsgBox(IntPtr.Zero, message, ":)", 0);

            result = Marshal.StringToBSTR("???"); 
        }

This required for interaction with Pascal/Delphi, where the return value is passed as parameter by reference when using stdcall: https://stackoverflow.com/a/9353800/4503491

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AaronRobinsonMSFTcommented, May 28, 2022

public static unsafe void ReturnString(IntPtr value, int* result)

@CoenraadS This is a bit questionable if your previous API was using out IntPtr. Instead this should be:

public static unsafe void ReturnString(IntPtr value, IntPtr* result)
{
    var message = Marshal.PtrToStringUni(value);
    _ = NativeMethods.MsgBox(IntPtr.Zero, message, ":)", 0);
    *result = Marshal.StringToBSTR("???");
}
0reactions
AaronRobinsonMSFTcommented, May 31, 2022

The default for x86 on Windows is stdcall but on non-Windows it is cdecl.

I think I will probably build a small code generation layer ontop of DNNE so I abstract away these pointers and put out & marshal types in the signature, but thought first I would ask if something perhaps already exists?

When you say “on top of”, I assume you mean to say that the method UnmanagedCallersOnly calls a method that uses the C# keyword - this is fine. If this isn’t the case, please elaborate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Exporting multiple functions with arguments
All I am trying to do is to export two functions from a module. One function taking an argument and the other with...
Read more >
Is there a way to export functions with parameters already ...
My first thought was copying the value of PARAMETER to a parameter with a unique name, like my_func_PARAMETER . This would most likely...
Read more >
Out Parameter With Examples in C# - ...
The out parameter is allowed to use var type in the method parameter list. In out parameter, it is not compulsory that the...
Read more >
I want to create a function module but
EXPORTING parameters are output parameters. When the function module is called, a suitable actual parameter can be specified for every output parameter.
Read more >
Testing Non-Exported Functions in JavaScript
To test exported function, you can simply import it. But what about non-exported functions? Well that's easy too, thanks to Rewire!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found