dll添加引用进来!里面的里面就可以调用乐呀
解决方案 »
- IHttpHandler 定义的模块 如何让某个网页不调用呢?用于后台用户登录验证的
- 为什么button的onclick事件没有执行呢?
- 在IE 下可以播放 WindowMedia 媒体文件 real媒体文件
- 将 GridView 中的内容导出到 Excel 中多个工作表(Sheet)
- .net 网站 发布问题,高分求助 急。。。
- 很有意思的SQL 查询
- 我现在把dataset从streamwriter写到excel上,但是只能保存在服务器端,怎么让它可以保存在客户端上?help
- 如何将TREEVIEW的节点设置为不可选
- 关于DIV元素vertical-align失效的问题!
- 装.net时弹出无法创建文件夹的错误是什么原因?
- 如何从程序传值给水晶报表?
- 高手请指教,,????!~
就可了
#include <iostream.h>extern "C"
{
__declspec(dllexport) void Test(void* v, int* n) {
char* c = (char*)v;
*n = 10;
for(int i=0; i < *n; i++)
c[i] = 'A'+i;
}
}2. TestVoid.cs:
using System;
using System.Runtime.InteropServices;class Class1
{
[STAThread]
static void Main(string[] args)
{
byte[] b = new byte[100];
int n;
Test(b,out n);
Console.WriteLine(n);
for (int i=0;i < n; i++)
Console.WriteLine((char)b[i]);
} [DllImport("TestVoid.dll")]
public static extern void Test(byte[] bs, out int size);
}
3. output:
E:\labs\csharp>TestVoid
10
A
B
C
D
E
F
G
H
I
J
4. if your dll has specical calling convention, then you need to use additional parameters for DllImport:[DllImport("msvcrt.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]see
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemRuntimeInteropServicesDllImportAttributeClassTopic.asphttp://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/vcwlkSysimportAttributeTutorial.asp
[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(string fileName) ;[DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr handle, string procName) ;protected void GetVariable()
{
IntPtr handle = LoadLibrary("Winscard") ;
IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci") ;
//How can I get the value of variable then, I don't know.
}
C:\Program Files\Microsoft Visual Studio\VC98\Bintry1. TestVoid.cpp:extern "C"
{
__declspec(dllexport) int g_rgSCardT0Pci = 100;
__declspec(dllexport) void Test(void* v, int* n, int *g) {
char* c = (char*)v;
*n = 10;
for(int i=0; i < *n; i++)
c[i] = 'A'+i;
*g = g_rgSCardT0Pci;
}
}
2. TestVoid.cs:
using System;
using System.Runtime.InteropServices;class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Starting...");
byte[] b = new byte[100];
int n;
int v;
Test(b,out n, out v);
Console.WriteLine(n);
for (int i=0;i < n; i++)
Console.WriteLine((char)b[i]); Console.WriteLine(v);
GetVariable();
}
unsafe static protected void GetVariable()
{
IntPtr handle = LoadLibrary("TestVoid.dll") ;
IntPtr pci = GetProcAddress(handle, "g_rgSCardT0Pci") ;
//Console.WriteLine("{0:X}", pci);
int* p = (int*)pci;
Console.WriteLine(*p);
FreeLibrary(handle);
}
[DllImport("TestVoid.dll")]
public static extern void Test(byte[] bs, out int size, out int v);
[DllImport("kernel32.dll")]
private extern static IntPtr LoadLibrary(string fileName) ; [DllImport("kernel32.dll")]
private extern static IntPtr GetProcAddress(IntPtr handle, string procName) ;
[DllImport("kernel32.dll")]
private extern static void FreeLibrary(IntPtr handle) ;
}