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