.net - Why does this call to AddDllDirectory fail with "Parameter is incorrect"? -
why following code not work?
open system open system.runtime.interopservices open system.componentmodel [<dllimport("kernel32")>] extern int adddlldirectory(string newdirectory) [<entrypoint>] let main argv = let result = adddlldirectory("c:\\") if result = 0 printfn "%a" <| win32exception(marshal.getlastwin32error()) // prints: "system.componentmodel.win32exception (0x80004005): parameter incorrect" system.console.readline() |> ignore 0 // return integer exit code
adddlldirectory() recent addition winapi. guaranteed available in windows 8, getting on earlier windows versions requires update, kb2533623. keep mind when select product requirements.
it unusual in more 1 way, doesn't follow normal pattern winapi functions accept string. makes function available in two versions, ansi version has appended , unicode version has w appended. adddlldirectory() has no appended letter, unicode version exists. isn't clear me whether intentional or oversight, high odds intentional. function declaration missing windows 8 sdk headers, unusual indeed.
so original declaration failed because called unicode version pinvoke marshaller passed ansi string. got lucky because string had odd number of characters enough lucky zeros not cause accessviolation.
using charset property in [dllimport] declaration required pinvoke marshaller passes unicode string.
Comments
Post a Comment