开发者

Simply declaring a function signature from an external DLL does not give a runtime exception if the DLL or function doesn't exist, right?

开发者 https://www.devze.com 2023-01-28 05:40 出处:网络
For example, if I\'m declaring: internal static class WinAPI { [DllImport(\"DwmApi.dll\", PreserveSig = false)]

For example, if I'm declaring:

internal static class WinAPI
{
    [DllImport("DwmApi.dll", PreserveSig = false)]
    internal static extern bool DwmIsCompositionEnabled();
}

but I'm not calling the DwmIsCompositionEnabled function in my project, I hope this will not开发者_高级运维 raise an exception when running the program on systems where DwmApi.dll does not exist (or the function does not exist in that DLL). (Real case: DwmApi.dll requires Windows Vista, so it doesn't exist on Windows XP).


This will only be a problem if and when the API is called.

You could consider a thin C# wrapper on the P/Invoke to facilitate handling the error, if you call this from multiple places.


As long as you don't call it or don't use NGEN for deployment (ie: if you always rely on .NET Jit), you're fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消