usedumpbin /exports dllsample2.dllto make sure the entrypoint is correct and then use some attrbutes like the following:
[DllImport("Simple.Dll", EntryPoint="?TestCDECL_CPP@@YAXXZ",
CallingConvention=CallingConvention.Cdecl )][DllImport("Simple.Dll", EntryPoint="?TestSTDCALL_CPP@@YGXXZ",
CallingConvention=CallingConvention.StdCall)]
[DllImport("Simple.Dll", EntryPoint="?TestCDECL_CPP@@YAXXZ",
CallingConvention=CallingConvention.Cdecl )][DllImport("Simple.Dll", EntryPoint="?TestSTDCALL_CPP@@YGXXZ",
CallingConvention=CallingConvention.StdCall)]
解决方案 »
- 要做一个手机短信查询数据的程序,或数据变更后发短信通知用户的程序,架构会是什么样?
- 项目建立了一个SQL CE项目,并且给我增加了个数据集怎么使用他?
- C#(windows control library)生成的DLL,为什么不能被asp.net(C#)调用?
- 控件下载!
- win2003密码问题
- 请各位看看下列代码,112不等于1.12*100,是不是微软的BUG
- 如何给出一40位的由数字组成的char型的随机数?
- 如何把一个Image变成灰的?
- 有没有一个获取win form应用程序已经启动的时间?高手指教
- c#里面画线段的困惑
- 请高手指点??(移动通信开发需要那些骇心技术??)
- 求助,两个操作系统,要想删除一个怎么删?
首先:
DllSample.cpp's code
#include "stdafx.h"
#include <stdio.h>
int _declspec(dllexport) Add(int a,int b);
void _declspec(dllexport) ShowString(); int _declspec(dllexport) Add(int a,int b)
{ return (a+b); } void _declspec(dllexport) ShowString()
{
printf("Nice to see you,I suceed!");
}
EXPORTS
Add
ShowString
// or project specific include files that are used frequently, but
// are changed infrequently
//#if !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
#define AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers#include <windows.h>
// TODO: reference additional headers your program requires here//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__A9DB83DB_A9FD_11D0_BFD1_444553540000__INCLUDED_)
照你的方法 我试了一下
结果如是:(我增加了一个函数Sub)F:\Microsoft Visual Studio\MyProjects\DllSample2\Debug>dumpbin /exports DllSample2.dll
Microsoft (R) COFF/PE Dumper Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file DllSample2.dllFile Type: DLL Section contains the following exports for DllSample2.dll 00000000 characteristics
3E786950 time date stamp Wed Mar 19 20:57:52 2003
0.00 version
1 ordinal base
4 number of functions
4 number of names ordinal hint RVA name 1 0 00001019 ??4Easy@@QAEAAV0@ABV0@@Z
2 1 0000100A ?Add@@YAHHH@Z
3 2 00001014 ?ShowString@@YAXXZ
4 3 0000100F ?Sub@@YAHHH@Z Summary 4000 .data
1000 .idata
3000 .rdata
2000 .reloc
28000 .textI suppose that the functions and classes are properly exported
but can you make some explanations to me,any thanks!
class DllSample
{
[DllImport("DllSample.DLL", EntryPoint="Add", SetLastError=true,
CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall)]
public static extern int Add(int a,int b); [DllImport("DllSample.DLL", EntryPoint="ShowString", SetLastError=true,
CharSet=CharSet.Ansi,
CallingConvention=CallingConvention.StdCall)]
public static extern void ShowString();
};调用如下没问题
MessageBox.Show(DllSample.Add(1,2).ToString());
DllSample.ShowString();
但是提示:“未能添加对‘....dll’的引用。这不是有效的程序集或COM组件。只有具有.dll后缀名的程序或COM组件才能被引用。请确保该文件可访问,并是一个有效的程序集或COM组件”
呵呵,
gdi32.dll和wsnmp32等等也无法以这个办法添加,但是这样子用就可以:
[DllImport("gdi32.dll")]
static public extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);[DllImport("wsnmp32")] public static extern int SnmpCleanup();搞不懂为什么
如果我的Dll中使用了自定义的结构,比如:
typedef struct node
{
int serialNumber;
char data[24];
struct node* pNext;
}Node,*pNode;而在导出函数中又以结构(或结构指针)为参数,例如:
void Insert_Node(pNode current_node);
那该怎么在.NET环境中使用呢?
我的邮件地址:[email protected]
猫崽一只