English 中文(简体)
Win32 api 调用错误在 f # 中获取短帕Name
原标题:Win32 api call Error GetShortPathName in f#
  • 时间:2012-05-24 18:39:01
  •  标签:
  • winapi
  • f#

我可以让这个代码在互动环境中运行, 但当我从调试器或.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"

最佳回答

固定了它。

我只是吸 @ 赢32

已删除的元帅

参见解决方案 :

open System.IO
open System.Runtime.InteropServices
open System.Text

[<DllImport("kernel32.dll",CharSet = CharSet.Auto, SetLastError=true)>]
extern int GetShortPathName(
    string longpath, 
    StringBuilder shortpath, 
    int  item)

let MakeShortName(longPath : string) =
    let sb =  StringBuilder()
    let currPath = longPath
    let item = 1024

   // 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
问题回答

Win32 函数可能会预设一个最大大小的预分配缓冲。 此代码固定了崩溃, 对我来说 :

let MakeShortName(longPath : string) =
    let maxSize = uint32 <| longPath.Length + 16
    let sb =  new StringBuilder("", int32 maxSize)
    let len = GetShortPathName(longPath, sb, maxSize)
    sb.ToString(0, int32 len)




相关问题
How to read exact number of bytes from a stream (tcp) socket?

In winsock, both the sync recv and the async WSARecv complete as soon as there is data available in a stream socket, regardless of the size specified (which is only the upper limit). This means that ...

AcquireCredentialsHandle returns SEC_E_NO_CREDENTIALS

I created a self-signed certificate (created using OpenSSL) and installed it into the Certificate Store using the Certificates MMC snap-in (CertMgr.msc) on Windows Vista Ultimate. I have managed to ...

Calling Win32 EnumThreadWindows() in C#

I m trying to get a call to EnumThreadWindows working, but I always get a Wrong Parameter-Error, although my code is nearly the same as this example on pinvoke.net. I don t know why this doesn t work: ...

COM Basic links

folks can you provide me the tutorial link or .pdf for learning basic COM?. i do google it.. still i recommend answers of stackoverflow so please pass me.. Thanks

Handling multiple windows WIN32 API

HI I m trying to create an application in the Win32 environment containing more than one window. How do i do that? all the Win32 Tutorials on web i found only showed how to manage one window. How do i ...

Creating a thread in DllMain?

It seems that when a thread is created from within DllMain upon DLL_PROCESS_ATTACH it won t begin until all dll s have been loaded. Since I need to make sure the thread runs before I continue, I get a ...

热门标签