求delphi调用vs下c写的dll实例 不要太简单,也不要太复杂,能够让我研究下的... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 自己写了一个,当作学习之用,你可以参考一下,用vc6.0+D7编写的。VC编写的DLL:#include <stdio.h>#include <windows.h>extern "C"{ _declspec(dllexport) int _cdecl getSum(int x, int y); _declspec(dllexport) int _cdecl getMax(int x, int y); _declspec(dllexport) int _cdecl getMin(int x, int y);}int getSum(int x, int y){ return x + y;}int getMax(int x, int y){ return (x>y) ? x : y;}int getMin(int x, int y){ return (x<y) ? x : y;}extern "C" BOOL APIENTRYDllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved){ if (dwReason == DLL_PROCESS_ATTACH) { MessageBox(0,"Dll Process Attach.","Dll Main",0); } else if (dwReason == DLL_PROCESS_DETACH) { MessageBox(0,"Dll Process Detach.","Dll Main",0); } return TRUE;}Delphi调用:unit unitMain;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;function getSum(x,y:Integer):Integer;cdecl; external 'DllDemo.dll';function getMax(x,y:Integer):Integer;cdecl; external 'DllDemo.dll';function getMin(x,y:Integer):Integer;cdecl; external 'DllDemo.dll';var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var a,b,c: Integer;begin a := 5; b := 4; c := getSum(a,b); ShowMessage('sum: ' + IntToStr(c)); c := getMax(a,b); ShowMessage('max: ' + IntToStr(c)); c := getMin(a,b); ShowMessage('min: ' + IntToStr(c));end;end. 能不能弄一个传递字符串的..用到pchar 和char * 哈哈,又是你,我给你写个,在vc的ide中新建一个空的dll项目,新建一个c++文件,把这些代码复制进去直接编译,我这里用了cstring为了省事,否则注意要转换宽字符显示否则乱码,中文要特别处理,你那个帖子我给你转换的代码了,msdn有详细的转换方法可以去看。另外你说的和字符集有关系,其实是没有的,因为char是基本类型,不管是unicode或mbcs或ansi,char就是char,就是-127-127的整数,但TCHAR和LPXXXSTR一类的数据类型是和字符集有关的。#include "atlstr.h"#include "windows.h"#define _C extern "C"_C _declspec(dllexport) int fun(char* a,char b,unsigned char c){ CString szstr=a; CString szPrompt; szPrompt.Format(_T("a是:%s\nb是:%c\nc是:%d"),szstr,b,c); MessageBoxW(0,szPrompt,0,0); return 0;}_C _declspec(dllexport) int Compare_String(char* a,char* b){ CString szstr1=a,szstr2=b; CString szPrompt; if(!strcmp(a,b)) szPrompt.Format(_T("字符串[%s]和字符串[%s]相同"),szstr1,szstr2); else szPrompt.Format(_T("字符串[%s]和字符串[%s]不相同"),szstr1,szstr2); MessageBoxW(0,szPrompt,0,0); return 0;}调用function fun(a:PChar;b:char;c:byte):integer;cdecl;external 'dll名';function Compare_String(a,b:PChar):integer;cdecl;external 'dll;实例:fun('我的字符串','c',255);Compare_String( '你好','你好');Compare_String( '你好','你不好'); 我其实也是最近为了改一个别人以前写的前台划价的程序,是用vc6写的mbcs编码,麻烦透了,正好也在弄这些编码问题,没办法,基础不好,新项目还是都用unicode把,既符合发展方向又简单。 我使用的是vc2008和delphi7,在d7里,pchar是指向ansi字符串的,我记得你好像用的是d2010,在高版本的d中可能pchar会默认指向宽字符串,听说d2007开始支持unicode了,具体我也没用过只是猜测,就是说你传进去的可能是个宽字符串,这样这个字符串的第二个字节就很可能是\0,char*会把2字节字符当成2个单字节字符,遇到\0就会认为字串已经结束了,造成只有第一个字节的字符,解决办法有2中,一是把你那个c++函数的参数改为wchar_t*代替char*,二是在d中先转换pwidechar到pansichar再传参,参看msdn中的WideCharToMultiByte函数。 DLL := LoadLibrary('BBBBBB.dll'); try if DLL <> 0 then begin @bb := GetProcAddress(DLL, 'Openoooo'); if @Obb <> nil then begin openxxxxx(12); end ; end ; finally FreeLibrary(fDLL); end; 我刚好早上已经领悟了....就是这个问题,.我在vc6.0和delphi7下是没问题的,哎.弄了这个问题这么长时间,郁闷 呵呵,光想着vc这边了,就没忘d那边想,问题解决了就好。 关于datasnap的一点傻傻的问题,谢谢各位大侠帮我一下 TSocketConnection 活动连接数问题!! delphi7的indy如何主动断开客户端 使用format函数出错 100 分 access +delphi 中的sql 问题。 学VCL和学用控件拖放编程是一回事吗? 怎么在memo中删除所有的空格,回车,然后在句号后面加回车? 北京通审公司招聘delphi程序员 听说DELPHI出了个DELPHI。NET。。情况如河? 谢谢各位,sorry.刚才我说的不太准确 财务账本的打印 为什么设置断点不报错,不设置断点就报错呢。
VC编写的DLL:#include <stdio.h>
#include <windows.h>extern "C"
{
_declspec(dllexport) int _cdecl getSum(int x, int y);
_declspec(dllexport) int _cdecl getMax(int x, int y);
_declspec(dllexport) int _cdecl getMin(int x, int y);}int getSum(int x, int y)
{
return x + y;
}int getMax(int x, int y)
{
return (x>y) ? x : y;
}int getMin(int x, int y)
{
return (x<y) ? x : y;
}extern "C" BOOL APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
MessageBox(0,"Dll Process Attach.","Dll Main",0);
}
else
if (dwReason == DLL_PROCESS_DETACH)
{
MessageBox(0,"Dll Process Detach.","Dll Main",0);
}
return TRUE;
}Delphi调用:unit unitMain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;function getSum(x,y:Integer):Integer;cdecl; external 'DllDemo.dll';
function getMax(x,y:Integer):Integer;cdecl; external 'DllDemo.dll';
function getMin(x,y:Integer):Integer;cdecl; external 'DllDemo.dll';var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
a,b,c: Integer;
begin
a := 5;
b := 4;
c := getSum(a,b);
ShowMessage('sum: ' + IntToStr(c));
c := getMax(a,b);
ShowMessage('max: ' + IntToStr(c));
c := getMin(a,b);
ShowMessage('min: ' + IntToStr(c));
end;end.
能不能弄一个传递字符串的..用到pchar 和char *
#include "windows.h"#define _C extern "C"_C _declspec(dllexport) int fun(char* a,char b,unsigned char c)
{
CString szstr=a;
CString szPrompt;
szPrompt.Format(_T("a是:%s\nb是:%c\nc是:%d"),szstr,b,c);
MessageBoxW(0,szPrompt,0,0);
return 0;
}_C _declspec(dllexport) int Compare_String(char* a,char* b)
{
CString szstr1=a,szstr2=b;
CString szPrompt;
if(!strcmp(a,b))
szPrompt.Format(_T("字符串[%s]和字符串[%s]相同"),szstr1,szstr2);
else
szPrompt.Format(_T("字符串[%s]和字符串[%s]不相同"),szstr1,szstr2); MessageBoxW(0,szPrompt,0,0);
return 0;
}
调用function fun(a:PChar;b:char;c:byte):integer;cdecl;external 'dll名';
function Compare_String(a,b:PChar):integer;cdecl;external 'dll;实例:
fun('我的字符串','c',255);
Compare_String( '你好','你好');
Compare_String( '你好','你不好');
try
if DLL <> 0 then
begin
@bb := GetProcAddress(DLL, 'Openoooo');
if @Obb <> nil then
begin
openxxxxx(12);
end ;
end ;
finally
FreeLibrary(fDLL);
end;