C调用C#dll [英] C call of a C# dll

查看:83
本文介绍了C调用C#dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


是否可以

1.从使用.NET制作的dll调用函数(C#)

2.来自用简单编写的程序(如:不是.NET)C或C ++?


更具体地说,这就是我所拥有的。


1.

C#文件Class.cs:


使用System; //和其他一些包


命名空间myns

{

公共类Class

{

public Class()

{

}


public static byte [] getByteArray(

字符串名称,

int index,

字符串格式,

ref int n

){

//代码返回一些字节数组。

}

}

}


编译它,我得到myns.dll。


2.

C文件mycaller.c:


#include< windows.h>

#include< stdio.h>

#include" mycaller.h"


typedef byte *(* FunctionPointer)(char *,int,char *,int *);


#ifdef __cplusplus

externC {

#endif


byte * myfunc(){


//加载库

HINSTANCE m_libraryHandle = LoadLibrary(" myns.dll");

if(m_libraryHandle!= NULL){

printf(" Library loaded\\\
;);

}

else {

printf(" Library not loaded\\\
);

返回NULL;

}


byte * result = NULL;


//获取函数指针

FunctionPointer functionptr =

(FunctionPointer)GetProcAddress(m_libraryHandle,

" getByteArray");


if(functionptr!= NULL){

printf(" Function pointer is OK\ n);;


int n = -1;

//从加载的库中调用函数

result =(* functionptr)(" fubar1",0," fubar2",& n);


//修改结果的一些代码

}

else {

printf(" Functi)在指针上是NULL \ n");

}


//释放图书馆

FreeLibrary(m_libraryHandle);

printf(" Library freed\\\
);


返回结果;

};


#ifdef __cplusplus

}

#endif


现在,问题。库已加载,但函数指针是

总是NULL。


1.如果方法调用Class.cs中的getByteArray()应该是externalized

莫名其妙

为了让电话成功?如果是这样,我该如何外化它?


2. GetProcAddress()函数的名称是错误的吗?我应该将

命名空间(myns)和类名(Class)放在方法名称前面吗?如果

那么,
(我的意思是我使用的是分隔符)?


3.还有别的什么?


我们将非常感谢所有帮助。谢谢。

解决方案

您可以将.NET类注册为COM类(请参阅regasm.exe)并使用CoCreateInstance和朋友来调用它 - 您可以使用tlbexp.exe为你的程序集创建一个类型库


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


各位大家好,


是否可以

1.用.NET编写的一个函数调用一个函数(C#)

2.用简单的程序编写的程序(如:不是.NET)C或C ++?


Richard Blewett [DevelopMentor]写道:

您可以将.NET类注册为COM类(请参阅regasm.exe)
并使用CoCreateInstance和朋友来调用它 - 您可以使用使用tlbexp.exe为你的程序集创建一个typelib




我是.NET和C#的新手。你可以更具体一点吗?


Richard Blewett [DevelopMentor]写道:

你可以将.NET类注册为COM类(请参阅regasm.exe)并使用CoCreateInstance和朋友来调用它 - 您可以使用tlbexp.exe为程序集创建一个类型库




请不要将答案发给Microsoft-具体问题comp.lang.c

dll',.NET,C#都是Gatesware,与C

编程语言无关。


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.

解决方案

You can register the .NET class as a COM class (see regasm.exe) and use CoCreateInstance and friends to call it - you can create a typelib for your assembly using tlbexp.exe

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

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++?


Richard Blewett [DevelopMentor] wrote:

You can register the .NET class as a COM class (see regasm.exe)
and use CoCreateInstance and friends to call it - you can create
a typelib for your assembly using tlbexp.exe



I''m fairly new to .NET and C#. Could you be more specific, please?


Richard Blewett [DevelopMentor] wrote:

You can register the .NET class as a COM class (see regasm.exe) and use CoCreateInstance and friends to call it - you can create a typelib for your assembly using tlbexp.exe



Please do not post answers to Microsoft-specific questions to comp.lang.c
dll''s, .NET, C# are all Gatesware and have nothing to do with the C
programming language.


这篇关于C调用C#dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆