I have these in my c++ header file
#ifndef S2dll_H
#define S2dll_H
#ifdef S2dll_EXPORTS
#define S2dll_API __declspec(dllexport)
#else
#pragma message("automatic link to S2dll.LIB")
#pragma comment(lib, "S2dll.lib")
#define S2dll_API __declspec(dllimport)
#endif
class is declared like this
class S2dll_API Sample
{
//members here
}
a cpp file containing function definitions, constructors
void * __stdcall CreateS() //constructor
{
return new SDLL;
}
void __stdcall DestroyS(void * objPtr) //destructor
{
s* sObj = (s *) objPtr;
if (sobj)
delete sObj;
}
exporting/exposing this function
void __stdcall setvaluesDLL(void *ptr, int x, int y,int s, int p)
{
Sample *dll = (Sample *) ptr;
if (dll)
{
dll->setposition(c); //functions in the cpp file
dll->setlocation(x,y);
dll->setsize(s);
}
}
a .def file
LIBRARY BS2dll
EXPORTS
CreateS
DestroyS
setvaluesDLL
so I am trying to access it in my c# win form
made this to expose it
static internal class dllcall
{
[DllImport(@"adrress\S2dll.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void setvaluesDLL(IntPtr ptr,int x, int y, int s, int p);
}
calling it in my winform
private void Assign_Click(object sender, EventArgs e)
{
dllcall.setvaluesDLL(ptr, x, y, s, p);//all values are int
}
I get this error: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
been searching google and staring at this code for hours and every time I manage to solve a problem, a new one comes out whenever I call setvaluesDLL(//parameters)
EDITED: IntPtr ptr is my main problem here and I have absolutely no idea how to use nor initialize it
You have to initialize first setvaluesDLL parameter value with CreateS() result. This method should be imported from the dll too. The same as DestroyS - to release memory properly