我可以让这个代码在互动环境中运行, 但当我从调试器或.exe 文件运行代码时, 它会崩溃 。
Forgot the error: FatalExecutionEngineError was detected! The runtime has encountered a fatal error. The address of the error was at 0x6c9781b0, on thread 0x1104. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
使用.net 4.5
open System.IO
open System.Runtime.InteropServices
open System.Text
[<DllImport("kernel32.dll",CharSet = CharSet.Auto, SetLastError=true)>]
extern uint32 GetShortPathName(
[<MarshalAs(UnmanagedType.LPWStr)>] string longpath,
[<MarshalAs(UnmanagedType.LPWStr)>] StringBuilder shortpath,
[<MarshalAs(UnmanagedType.U4)>] uint32 item)
let MakeShortName(longPath : string) =
let sb = StringBuilder()
let currPath = longPath
let item = 1024u
// let blah = ""
//win32 assigns shortPath
let blah32 = GetShortPathName(currPath, sb, item)
sb.ToString()
[<EntryPoint>]
let main argv =
let path = @"C:devshortNameshortName"
let shorty = MakeShortName path
printfn "%s" shorty
let x = System.Console.ReadKey()
0
交互式封封( V)
$(me alt+加入上述两个功能)
val GetShortPathName : string * StringBuilder * uint32 -> uint32 val MakeShortName : string -> string
$ MakeShortName @"C:devshortNameshortName";; val it : string = "C:devSHORTN~1SHORTN~1"