R
russb_69
Russ here. I'm new to this forum. I need help in understanding why
the following code does not work. Basically, I'm defining a
structure in a c# application, and using a c++ dll to modify it.
Here's the c++ .dll file:
// DemoAccountDLLTest.cpp : Defines the entry point for the DLL
application.
//
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
struct DemoAccountInfo
{
int login;
char password[16];
char phonepassword[16];
char name[64];
char group[16];
char country[32];
char city[32];
char state[32];
char zipcode[16];
char address[32];
char phone[32];
char email[32];
char unused[72];
int leverage;
double balance;
char reserved[20];
};
extern "C" __declspec(dllexport) void __stdcall
OpenDemoAccount(DemoAccountInfo *info)
{
info->login = 123456;
strcpy(info->password, "a1b2c3d4");
}
Here's the c# console application:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("DemoAccountDLLTest.dll")]
public static extern void OpenDemoAccount(out DemoAccountInfo
info);
[StructLayout(LayoutKind.Sequential)]
public struct DemoAccountInfo
{
public int login;
public char[] password;
public char[] phonepassword;
public char[] name;
public char[] group;
public char[] country;
public char[] city;
public char[] state;
public char[] zipcode;
public char[] address;
public char[] phone;
public char[] email;
public char[] unused;
public int leverage;
public double balance;
public char[] reserved;
};
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
DemoAccountInfo acc = new DemoAccountInfo();
acc.name = "John Smith".ToCharArray();
acc.email = "(e-mail address removed)".ToCharArray();
acc.address = "some address".ToCharArray();
acc.city = "Toronto".ToCharArray();
acc.country = "Canada".ToCharArray();
acc.state = "Ontario".ToCharArray();
acc.phone = "777777777".ToCharArray();
acc.zipcode = "123456".ToCharArray();
acc.group = "demo".ToCharArray();
acc.balance = 100;
acc.leverage = 10;
OpenDemoAccount(out acc);
Console.WriteLine(acc.name);
Console.WriteLine(acc.login);
Console.WriteLine(acc.password);
Console.WriteLine("Finished.");
Console.ReadLine();
}
}
}
I think it may have something to do with the fact that I'm passing the
DemoAccountInfo parameter as a pointer in the .dll file and I'm
declaring it incorectly in c#. I may also have to implement somekind
of marshaling as well, but I'm just not sure. Keep in mind that the
code in the .dll file is programmed correctly and fallows design
specs, therefore it cannot be changed.
the following code does not work. Basically, I'm defining a
structure in a c# application, and using a c++ dll to modify it.
Here's the c++ .dll file:
// DemoAccountDLLTest.cpp : Defines the entry point for the DLL
application.
//
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
struct DemoAccountInfo
{
int login;
char password[16];
char phonepassword[16];
char name[64];
char group[16];
char country[32];
char city[32];
char state[32];
char zipcode[16];
char address[32];
char phone[32];
char email[32];
char unused[72];
int leverage;
double balance;
char reserved[20];
};
extern "C" __declspec(dllexport) void __stdcall
OpenDemoAccount(DemoAccountInfo *info)
{
info->login = 123456;
strcpy(info->password, "a1b2c3d4");
}
Here's the c# console application:
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[DllImport("DemoAccountDLLTest.dll")]
public static extern void OpenDemoAccount(out DemoAccountInfo
info);
[StructLayout(LayoutKind.Sequential)]
public struct DemoAccountInfo
{
public int login;
public char[] password;
public char[] phonepassword;
public char[] name;
public char[] group;
public char[] country;
public char[] city;
public char[] state;
public char[] zipcode;
public char[] address;
public char[] phone;
public char[] email;
public char[] unused;
public int leverage;
public double balance;
public char[] reserved;
};
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
DemoAccountInfo acc = new DemoAccountInfo();
acc.name = "John Smith".ToCharArray();
acc.email = "(e-mail address removed)".ToCharArray();
acc.address = "some address".ToCharArray();
acc.city = "Toronto".ToCharArray();
acc.country = "Canada".ToCharArray();
acc.state = "Ontario".ToCharArray();
acc.phone = "777777777".ToCharArray();
acc.zipcode = "123456".ToCharArray();
acc.group = "demo".ToCharArray();
acc.balance = 100;
acc.leverage = 10;
OpenDemoAccount(out acc);
Console.WriteLine(acc.name);
Console.WriteLine(acc.login);
Console.WriteLine(acc.password);
Console.WriteLine("Finished.");
Console.ReadLine();
}
}
}
I think it may have something to do with the fact that I'm passing the
DemoAccountInfo parameter as a pointer in the .dll file and I'm
declaring it incorectly in c#. I may also have to implement somekind
of marshaling as well, but I'm just not sure. Keep in mind that the
code in the .dll file is programmed correctly and fallows design
specs, therefore it cannot be changed.