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. Calling C++ DLL from C# program

Calling C++ DLL from C# program

Scheduled Pinned Locked Moved C#
csharpc++performancehelp
4 Posts 3 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.
  • F Offline
    F Offline
    francois_t
    wrote on last edited by
    #1

    I'm trying to call a C++ dll from my C# (VS2005) application and keep on getting the same exception: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Here's the code: [C++] typedef void * SessionHandle; swint16 sw_createsession(const char * ConfigName,SessionHandle * hSes,void *userdef,const char *inipath); I tried this, but it does not work: [C#] Wrapper code: [DllImport("MyDLL.dll")] public static extern short sw_createsession(string configName, [In][Out] ref IntPtr hSession, IntPtr configFunction, string iniPath); Function call: configName and iniPath is declared as strings and _hSession and userdef is declared as IntPtr's and initialized to IntPtr.Zero Code: short result = SwitchAPIWrapper.sw_createsession(configName, ref _hSession,ref userdef,iniPath); Hope this makes sense. If anyone can help me with this one, it would be great. Thanx!

    C 1 Reply Last reply
    0
    • F francois_t

      I'm trying to call a C++ dll from my C# (VS2005) application and keep on getting the same exception: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Here's the code: [C++] typedef void * SessionHandle; swint16 sw_createsession(const char * ConfigName,SessionHandle * hSes,void *userdef,const char *inipath); I tried this, but it does not work: [C#] Wrapper code: [DllImport("MyDLL.dll")] public static extern short sw_createsession(string configName, [In][Out] ref IntPtr hSession, IntPtr configFunction, string iniPath); Function call: configName and iniPath is declared as strings and _hSession and userdef is declared as IntPtr's and initialized to IntPtr.Zero Code: short result = SwitchAPIWrapper.sw_createsession(configName, ref _hSession,ref userdef,iniPath); Hope this makes sense. If anyone can help me with this one, it would be great. Thanx!

      C Offline
      C Offline
      Chris Patrick
      wrote on last edited by
      #2

      the import code looks fine... check if the C++ dll is read-only, maybe the problem is something like this(silly that is!)

      I can do programs, but i can't program my life!

      F 1 Reply Last reply
      0
      • C Chris Patrick

        the import code looks fine... check if the C++ dll is read-only, maybe the problem is something like this(silly that is!)

        I can do programs, but i can't program my life!

        F Offline
        F Offline
        francois_t
        wrote on last edited by
        #3

        Nope, not read-only. I think it's got something to do with the new security features of Visual Studio 2005, but I'm not sure it's as if when I call the c++ function, it sets the _hsession variable and then throws an exception. Maybe my reference to the void* is incorrect? I'm new at this and it's been driving me crazy for almost a month now. :((

        C 1 Reply Last reply
        0
        • F francois_t

          Nope, not read-only. I think it's got something to do with the new security features of Visual Studio 2005, but I'm not sure it's as if when I call the c++ function, it sets the _hsession variable and then throws an exception. Maybe my reference to the void* is incorrect? I'm new at this and it's been driving me crazy for almost a month now. :((

          C Offline
          C Offline
          ChrisKiki
          wrote on last edited by
          #4

          the error could be from uninitialized pointers. U have to initialize any pointer you use in C++.

          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