B
Brian
I am very new to C# programming and have run into a problem.
I apologize for the repost of this article. For some reason, my news
reader attached it to an existing thread.
First off, I have an SDK that I have written for C/C++ and would like to
port it to C# if at all possible.
Basically, I've got a structure that I need to pass to the C-style DLL
(i.e. no mangled names - everything is __stdcall). This structure needs
to contain pointers to callback functions that the DLL can use to notify
the C# application when certain events occur. In addition, I need to use
SetEnvironmentVariable and pass a string containing a pointer to a
character array for special processing by the DLL. I've included some
code that shows basically everything I'm looking to do.
To save time, I understand how to get access to exported functions within
DLLs.
// this structure will be filled by the DLL and will be passed
// to the callback for function2, (i.e. FnPtr2's typedef)
typedef struct _toPassAsArgumentStruct
{
int a;
int b;
double c;
} TO_PASS_AS_ARGUMENT;
// these are the typedef's for two callback functions that will be
// stored in the structure, DEMOSTRUCT
typedef BOOL (__stdcall *FnPtr1)(DWORD i, char *path, DWORD pathLength);
typedef BOOL (__stdcall *FnPtr2)(TO_PASS_AS_ARGUMENT *structure);
typedef struct _demoStruct
{
FnPtr1 function1;
FnPtr2 function2;
} DEMOSTRUCT;
char global_text[5] = {"test"};
// MyFunction1 and MyFunction2 are called by the DLL
BOOL MyFunction1(DWORD i, char *path, DWORD pathLength)
{
strcpy(path, "unknown");
return TRUE;
}
BOOL MyFunction2(TO_PASS_AS_ARGUMENT *structure)
{
structure->a = 5;
return TRUE;
}
// simple_function is called by the non-DLL (i.e. the application)
void simple_function()
{
char text[128] = {0};
DEMOSTRUCT ds = {0};
// Are these four statements possible in C#?
ds.function1 = MyFunction1;
ds.function2 = MyFunction2;
// the contents of 'text' will be passed to the DLL.
sprintf(text, "%x", &global_text);
SetEnvironmentVariable("test", text);
}
I apologize for the repost of this article. For some reason, my news
reader attached it to an existing thread.
First off, I have an SDK that I have written for C/C++ and would like to
port it to C# if at all possible.
Basically, I've got a structure that I need to pass to the C-style DLL
(i.e. no mangled names - everything is __stdcall). This structure needs
to contain pointers to callback functions that the DLL can use to notify
the C# application when certain events occur. In addition, I need to use
SetEnvironmentVariable and pass a string containing a pointer to a
character array for special processing by the DLL. I've included some
code that shows basically everything I'm looking to do.
To save time, I understand how to get access to exported functions within
DLLs.
// this structure will be filled by the DLL and will be passed
// to the callback for function2, (i.e. FnPtr2's typedef)
typedef struct _toPassAsArgumentStruct
{
int a;
int b;
double c;
} TO_PASS_AS_ARGUMENT;
// these are the typedef's for two callback functions that will be
// stored in the structure, DEMOSTRUCT
typedef BOOL (__stdcall *FnPtr1)(DWORD i, char *path, DWORD pathLength);
typedef BOOL (__stdcall *FnPtr2)(TO_PASS_AS_ARGUMENT *structure);
typedef struct _demoStruct
{
FnPtr1 function1;
FnPtr2 function2;
} DEMOSTRUCT;
char global_text[5] = {"test"};
// MyFunction1 and MyFunction2 are called by the DLL
BOOL MyFunction1(DWORD i, char *path, DWORD pathLength)
{
strcpy(path, "unknown");
return TRUE;
}
BOOL MyFunction2(TO_PASS_AS_ARGUMENT *structure)
{
structure->a = 5;
return TRUE;
}
// simple_function is called by the non-DLL (i.e. the application)
void simple_function()
{
char text[128] = {0};
DEMOSTRUCT ds = {0};
// Are these four statements possible in C#?
ds.function1 = MyFunction1;
ds.function2 = MyFunction2;
// the contents of 'text' will be passed to the DLL.
sprintf(text, "%x", &global_text);
SetEnvironmentVariable("test", text);
}