E
Elstree
Hi everybody,
Is it possible to
1. call a function from a dll made with .NET (C#)
2. from a program written in plain (as in: not .NET) C or C++?
To be more specific, this is what I have.
1.
C# file Class.cs:
using System; //and some other packages
namespace myns
{
public class Class
{
public Class()
{
}
public static byte[] getByteArray(
string name,
int index,
string format,
ref int n
) {
//Code returning some byte array.
}
}
}
Compiling it, I get myns.dll.
2.
C file mycaller.c:
#include<windows.h>
#include<stdio.h>
#include"mycaller.h"
typedef byte* (*FunctionPointer)(char*, int, char*, int*);
#ifdef __cplusplus
extern "C" {
#endif
byte* myfunc() {
//load the library
HINSTANCE m_libraryHandle = LoadLibrary("myns.dll");
if(m_libraryHandle != NULL) {
printf("Library loaded\n");
}
else {
printf("Library not loaded\n");
return NULL;
}
byte* result = NULL;
//get the function pointer
FunctionPointer functionptr =
(FunctionPointer)GetProcAddress(m_libraryHandle,
"getByteArray");
if(functionptr != NULL) {
printf("Function pointer is OK\n");
int n = -1;
//call the function from the loaded library
result = (*functionptr)("fubar1", 0, "fubar2", &n);
//some code modifying the result
}
else {
printf("Function pointer is NULL\n");
}
//free the library
FreeLibrary(m_libraryHandle);
printf("Library freed\n");
return result;
};
#ifdef __cplusplus
}
#endif
Now, the problem. Library gets loaded but the function pointer is
allways NULL.
1. Should the method call getByteArray() in Class.cs be "externalized"
somehow
in order for the call to be successful? If so, how do I "externalize" it?
2. Is the name of the function in GetProcAddress() wrong? Should I put the
namespace (myns) and class name (Class) in front of the method name? If
so, how (I mean what delimiters do I use)?
3. Something else?
All help will be greatly appreciated. Thanks.
Is it possible to
1. call a function from a dll made with .NET (C#)
2. from a program written in plain (as in: not .NET) C or C++?
To be more specific, this is what I have.
1.
C# file Class.cs:
using System; //and some other packages
namespace myns
{
public class Class
{
public Class()
{
}
public static byte[] getByteArray(
string name,
int index,
string format,
ref int n
) {
//Code returning some byte array.
}
}
}
Compiling it, I get myns.dll.
2.
C file mycaller.c:
#include<windows.h>
#include<stdio.h>
#include"mycaller.h"
typedef byte* (*FunctionPointer)(char*, int, char*, int*);
#ifdef __cplusplus
extern "C" {
#endif
byte* myfunc() {
//load the library
HINSTANCE m_libraryHandle = LoadLibrary("myns.dll");
if(m_libraryHandle != NULL) {
printf("Library loaded\n");
}
else {
printf("Library not loaded\n");
return NULL;
}
byte* result = NULL;
//get the function pointer
FunctionPointer functionptr =
(FunctionPointer)GetProcAddress(m_libraryHandle,
"getByteArray");
if(functionptr != NULL) {
printf("Function pointer is OK\n");
int n = -1;
//call the function from the loaded library
result = (*functionptr)("fubar1", 0, "fubar2", &n);
//some code modifying the result
}
else {
printf("Function pointer is NULL\n");
}
//free the library
FreeLibrary(m_libraryHandle);
printf("Library freed\n");
return result;
};
#ifdef __cplusplus
}
#endif
Now, the problem. Library gets loaded but the function pointer is
allways NULL.
1. Should the method call getByteArray() in Class.cs be "externalized"
somehow
in order for the call to be successful? If so, how do I "externalize" it?
2. Is the name of the function in GetProcAddress() wrong? Should I put the
namespace (myns) and class name (Class) in front of the method name? If
so, how (I mean what delimiters do I use)?
3. Something else?
All help will be greatly appreciated. Thanks.