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. Insanely simple interop question.

Insanely simple interop question.

Scheduled Pinned Locked Moved C#
helpcsharphtmlcomquestion
3 Posts 2 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
    Bog
    wrote on last edited by
    #1

    All I want to do is: I want to make a win32 DLL that takes two LPWSTR's. One [in], one [out]. That's ALL! The problem is, when I call it from C#, the out string is always unchanged. I know this must be simple to do- interop can't suck this bad. Here is my code: /// In the DLL: extern "C" { __declspec(dllexport) void transformIt(LPWSTR,[out] LPWSTR); void transformIt(LPWSTR markup,[out] LPWSTR changed) { changed= L"SOME STUFF"; } } // The Interop and the call: [System.Runtime.InteropServices.DllImportAttribute("mydll.dll")] private static extern void transformIt( [MarshalAs(UnmanagedType.LPWStr)] string markup, [Out,MarshalAs(UnmanagedType.LPWStr)] out string changed) ; string stuff="wefwefw"; string changed; //also tried string changed="",changed=null, and a StringBuilder transformIt( stuff, out changed); Console.WriteLine(" >>>>>>>>>>>>>>> " + changed); //empty I've tried this with a StringBuilder, I've tried with "ref" instead of an "out". No error is thrown, even when I do a try catch block. I know it's finding the DLL ok, becaause I made another test method that just returns an int and it worked. why why why why why why why Please tell me it's me that sucks and not interop. "Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read." -Groucho Marx

    R 1 Reply Last reply
    0
    • B Bog

      All I want to do is: I want to make a win32 DLL that takes two LPWSTR's. One [in], one [out]. That's ALL! The problem is, when I call it from C#, the out string is always unchanged. I know this must be simple to do- interop can't suck this bad. Here is my code: /// In the DLL: extern "C" { __declspec(dllexport) void transformIt(LPWSTR,[out] LPWSTR); void transformIt(LPWSTR markup,[out] LPWSTR changed) { changed= L"SOME STUFF"; } } // The Interop and the call: [System.Runtime.InteropServices.DllImportAttribute("mydll.dll")] private static extern void transformIt( [MarshalAs(UnmanagedType.LPWStr)] string markup, [Out,MarshalAs(UnmanagedType.LPWStr)] out string changed) ; string stuff="wefwefw"; string changed; //also tried string changed="",changed=null, and a StringBuilder transformIt( stuff, out changed); Console.WriteLine(" >>>>>>>>>>>>>>> " + changed); //empty I've tried this with a StringBuilder, I've tried with "ref" instead of an "out". No error is thrown, even when I do a try catch block. I know it's finding the DLL ok, becaause I made another test method that just returns an int and it worked. why why why why why why why Please tell me it's me that sucks and not interop. "Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read." -Groucho Marx

      R Offline
      R Offline
      Ranjan Banerji
      wrote on last edited by
      #2

      Have you tested your DLL in a non .Net environment? Maybe something is just not right with the DLL.

      B 1 Reply Last reply
      0
      • R Ranjan Banerji

        Have you tested your DLL in a non .Net environment? Maybe something is just not right with the DLL.

        B Offline
        B Offline
        Bog
        wrote on last edited by
        #3

        Well like I say, I tried making a simple function that just returns an int, in the same dll, and calling that from dot net and it worked fine. Hey can you tell me if I'm assigning the LPWSTR properly? Do I have to do like memory alloc or something, or make a char array? The compiler didn't throw any errors.. thanks "Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read." -Groucho Marx

        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