#include
#include
#define _e extern "C" __declspec(dllexport)
#define _i extern "C" __declspec(dllimport)
struct Args {
std::string ip;
std::string uparams;
};
_e std::string PAGE(Args args)
{
std::string arguments = args.uparams;
std::string buffer = "Hello World!!! <<--->> arguments -> \"";
buffer += arguments;
buffer += "\"";
return buffer;
}
[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
static extern int LoadLibrary(
[MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
static extern IntPtr GetProcAddress(int hModule,
[MarshalAs(UnmanagedType.LPStr)] string lpProcName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
static extern bool FreeLibrary(int hModule);
struct Arguments
{
public string ip;
public string uparams;
};
delegate string Str([MarshalAs(UnmanagedType.Struct)] Arguments args);
static void Main(string[] args)
{
int a = LoadLibrary("C:\\CTEST.dll");
if (a < 1) {
Console.Write("Cannot load library");
Console.Read();
return;
}
Console.WriteLine("Library Loaded");
Console.Read();
Str st = (Str)Marshal.GetDelegateForFunctionPointer(GetProcAddress(a, "PAGE"), typeof(Str));
Arguments ar;
ar.ip = "127.0.0.1";
ar.uparams = "Hell Yeah";
Console.WriteLine(st(ar));
FreeLibrary(a);
}Attempted to read or write protected memory. This is often an indication that other memory is corrupt
L.E Sorry for the bad section of my other thread.
Serban CosminPosted Jun 15, 2010, 3:09 AM
I think you need to declare your struct with sequential layout, using this attribute:
[StructLayout(LayoutKind.Sequential)]
Hope this helps.