Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

c# - Set DllImport attribute dynamically

I am making use of an external unmanaged dll using PInvoke and the DllImport attribute. eg.

[DllImport("mcs_apiD.dll", CharSet = CharSet.Auto)]
private static extern byte start_api(byte pid, byte stat, byte dbg, byte ka);

I am wondering if it is possible to alter the dll file details (mcs_apiD.dll in this example) dynmically in some manner, if for instance I wanted to build against another dll version

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes this is possible, you'll have to do part of the job that the P/Invoke marshaller does. Loading the DLL and finding the entry point of the exported function. Start by declaring a delegate whose signature matches the exported function:

 private delegate byte start_api(byte pid, byte stat, byte dbg, byte ka);

Then use code like this:

 using System.ComponentModel;
 using System.Runtime.InteropServices;
  ...

    static IntPtr dllHandle;
  ...
        if (dllHandle == IntPtr.Zero) {
            dllHandle = LoadLibrary("mcs_apiD.dll");
            if (dllHandle == IntPtr.Zero) throw new Win32Exception();
        }
        IntPtr addr = GetProcAddress(dllHandle, "_start_api@16");
        if (addr == IntPtr.Zero) throw new Win32Exception();
        var func = (start_api)Marshal.GetDelegateForFunctionPointer(addr, typeof(start_api));
        var retval = func(1, 2, 3, 4);
  ...
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr LoadLibrary(string name);
    [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    private static extern IntPtr GetProcAddress(IntPtr hModule, string name);

Lots of ways to get this wrong of course. Do note that you have to use the actual exported name from the DLL, you no longer get the help from the P/Invoke marshaller to help with name decoration. Use dumpbin.exe /exports on the DLL if you are not sure what the export name looks like.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...