Calling SendMessage (P/Invoke) keeps crashing

前端 未结 3 1142
余生分开走
余生分开走 2021-01-06 07:35

I am having to write an application that communicates with a third-party program (AOL, I\'m sorry. :()

Doing a lot of research I found some ways to do this with P/In

3条回答
  •  伪装坚强ぢ
    2021-01-06 08:15

    I was getting a crash from Marshal.PtrToStringUni(bf) statement in similar situation where SendMessage was returning a "wrong size" for a text length with WM_GETTEXTLENGTH argument (the control class was "RICHEDIT50W"; multi-line text).

    I had tried adding 1, 10, 100 (to text length query result) and still would get an error even though (later on) the text length was equal what was returned from the first call (WM_GETTEXTLENGTH).

    My solution was: I multiplied the result with 2 then I trimmed it.

    I did use Marshal.AllocHGlobal(sz) and then Marshal.Release(bf), so there was no problem with memory efficiency. My guess is that for multi-line texts Marshal.AllocHGlobal(sz) wasn't making enough space in the memory even with exact text size (+1).

    Maybe the return character within the text (vbCr, vbLf) requires more memory: I found nothing to explain this isue, but doubling the size worked for me.

提交回复
热议问题