Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Please Help

Please Help

Scheduled Pinned Locked Moved C#
helpcsharpperformancequestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Beringer
    wrote on last edited by
    #1

    I am trying to access some functionality of the underlying RichTextBox control and am getting all mixed up with conversions of types. I know my basic issue is not completely understanding types in C#. I would be much obliged if someone could "educate" me some. I appoligize already for the amount of text in this message. Italic text is unmanged code and bold text is my attempt to write the same thing in C#. Since I need to use the SendMessage of User32.dll I defined the following: [DLLImport ("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); Then I'm trying to create the equivalent code in C# for the following callback function: DWORD EditStreamCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb); by defining the following delegate: public delegate UInt32 EditStreamCallback(IntPtr dwCookie, IntPtr pbBuff, Int32 cb, IntPtr pcb); That seems to work well (at least for the compiler). I then "translated" the following typedef: typedef struct _editstream { DWORD_PTR dwCookie; DWORD dwError; EDITSTREAMCALLBACK pfnCallback; } EDITSTREAM into the following structure: private struct EDITSTREAM { public IntPtr dwCookie; public UInt32 dwError; public EditStreamCallback pfnCallback; } So with all the definitions out of the way I thought I could do something like this: ... FileStream fs = new FileStream(Filename, FileMode.Open); int format = SF_RTF;EDITSTREAM es = new EDITSTREAM(); es.dwCookie = (IntPtr)fs; es.pfnCallback = new EditStreamCallback(StreamIn); SendMessage(this.Handle, EM_STREAMIN, (IntPtr)format, (IntPtr)es); ... but now the complier isn't happy and I get "Cannot convert type 'x' to System.IntPtr" errors where I try to cast to IntPtr. It was my understanding the "new" creates a reference type (i.e. memory address, e.g. pointer) So, why can't I cast to IntPtr here? It is interesting to note that there is no error on the cast of "format". Now on the flip side I have the following code: static UInt32 StreamIn(IntPtr dwCookie, IntPtr pbBuff, Int32 cb, IntPtr pcb) { UInt32 result = 0; FileStream fs = (FileStream)dwCookie; pcb = cb; try { pcb = fs.Read((byte[])pbBuff, 0, cb); } catch (Exception e) { pcb = 0; result = 1; } return result; } This is the callback function delegate used above. I get compile time errors at the attempted unboxing of dwCookie and pbBuff. Also there is

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups