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

Support custom Dll Import search path for entry point functions #1029

Closed
youyuanwu opened this issue Aug 27, 2023 · 4 comments
Closed

Support custom Dll Import search path for entry point functions #1029

youyuanwu opened this issue Aug 27, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@youyuanwu
Copy link

The generated code marks the dll import search path to be system32 path.
But the dll lives in C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code

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);
    }
}

So, when using these functions, this error is encountered.
The specified module could not be found.

The solution is to support custom values for "DefaultDllImportSearchPaths" in the generated code.

More details can be found here:
youyuanwu/service-fabric-cs#2

@youyuanwu youyuanwu added the enhancement New feature or request label Aug 27, 2023
@AArnott
Copy link
Member

AArnott commented Sep 1, 2023

Would specifying System32 | ApplicationDirectory | AssemblyDirectory be sufficient here? If not, what would be required?

@youyuanwu
Copy link
Author

youyuanwu commented Sep 6, 2023

FabricCreateLocalClient function comes from FabricClient.dll. This dll lives inside directory C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code and is in the system env:PATH variable.

So to make this work the code needs to find FabricClient.dll in the right directory.
I am not familiar with the dll searching in cs. But given the generated code, I think

[DefaultDllImportSearchPaths("C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code")] 

might work?
Is it possible to look up dll in the system PATH like native application?

@AArnott
Copy link
Member

AArnott commented Sep 7, 2023

You can't specify a path like that. The attribute only takes a flags enum. You can see the options here.

I'm not aware of any setting that will simply search all the directories in PATH for dependencies, even in native applications. I think native apps have the same set of options that is exposed to .NET applications through that enum.
Note that, per the docs in the enum, an AddDllDirectory win32 function exists whereby you can explicitly add a directory to the dll search path. Then you'd need to set the UserDirectories flag on the enum so that .NET would honor that.

Frankly though, I think the simplest path for you may be to find the file yourself and load it using LoadLibrary. Once loaded, these p/invokes will work regardless of this enum.

@youyuanwu
Copy link
Author

Using "LoadLibrary" suggested solved the issue.

@AArnott AArnott closed this as not planned Won't fix, can't repro, duplicate, stale Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants