Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with CsWin32 generated entry point function #2

Closed
youyuanwu opened this issue Jul 29, 2023 · 3 comments
Closed

Problem with CsWin32 generated entry point function #2

youyuanwu opened this issue Jul 29, 2023 · 3 comments

Comments

@youyuanwu
Copy link
Owner

In idl file, the entry point function is declared like this:

    [
        uuid(fefc1487-3c52-4ea1-a8eb-19cd7ec533e6),
        dllname("FabricClient.dll")
    ]
    module FabricClientModule {
        [entry("FabricCreateLocalClient")] HRESULT FabricCreateLocalClient(
            [in] REFIID iid,
            [out, retval] void ** fabricClient);
     }

The generated code is:

namespace Microsoft.ServiceFabric
{
    [GeneratedCode("Microsoft.Windows.CsWin32", "0.3.18-beta+dc807e7787")]
    public static class PInvoke
    {
        public static HRESULT FabricCreateLocalClient(in Guid iid, out void* fabricClient);
        [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
        public static HRESULT FabricCreateLocalClient(Guid* iid, void** fabricClient);
    }
}

One can use the second (raw types) function like this (not sure if it is correct):

unsafe {
    var guid = typeof(IFabricQueryClient).GUID;
    var clientRaw = IntPtr.Zero;
    HRESULT hr = PInvoke.FabricCreateLocalClient(&guid, (void**)&clientRaw);

    Debug.Assert(hr.Succeeded);
   
    var cc = Marshal.PtrToStructure<IFabricQueryClient>(clientRaw);
    Debug.Assert(cc != null);
};

But it is unclear how to use the first with out void*.

The other problem is that the dll cannot be loaded:

> dotnet run --project .\src\examples\examples.csproj        
Unhandled exception. System.DllNotFoundException: Unable to load DLL 'FabricClient' or one of its dependencies: The specified module could not be found. (0x8007007E)
   at Microsoft.ServiceFabric.PInvoke.FabricCreateLocalClient(Guid* iid, Void** fabricClient)
   at Program.<Main>$(String[] args) in D:\code\cs\service-fabric-cs\src\examples\Program.cs:line 29

Service Fabric dlls are in system path, but in directory C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code. Maybe the DefaultDllImportSearchPaths should be customized.
Maybe related to microsoft/CsWin32#968

@youyuanwu
Copy link
Owner Author

@AArnott @riverar for CsWin32 help again. Thanks.

@AArnott
Copy link

AArnott commented Aug 2, 2023

Interesting. We touched close to this in microsoft/CsWin32#617, but it was a different use case and the fix won't help here. Can you file a new issue against CsWin32?

@youyuanwu
Copy link
Owner Author

Following the suggestion in microsoft/CsWin32#1029 to use LoadLibrary directly, the dll is loaded and used by PInvoke.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants