delphi调用tuxedo源码转为C#,无法正常调用

zhoupq 2009-03-25 07:54:06
delphi代码:

var
Form1: TForm1;
vstr:string;
LIntRecv: integer; //tp执行情况
sendChaine, getChaine: pChar;
sendLentgh: integer;
i:integer;
TmpLg: Longint;
pChServiceName: pChar;
bufferType: string;
strSerName, strParam: string;
strResult: string;
vRetData:string;
implementation
function tpinit(tpinfo: longint): integer; stdcall; external 'c:\tux\wtuxws32.dll' name 'tpinit';
function tpterm: integer; stdcall; external 'c:\tux\wtuxws32.dll' name 'tpterm';
procedure tpfree(ptr: pchar); stdcall; external 'c:\tux\wtuxws32.dll' name 'tpfree';
function tpalloc(types: pChar; subtype: pchar; size: longint): pchar; stdcall; external 'c:\tux\wtuxws32.dll' name 'tpalloc';
function tpcall(svc: pChar; idata: pchar; ilen: longint; odata: pointer; olen: pointer; flags: longint): integer; stdcall;
external 'c:\tux\wtuxws32.dll' name 'tpcall';
function gettperrno(): integer; stdcall; external 'c:\tux\wtuxws32.dll' name 'gettperrno';
function tpstrerror(errno: integer): string; stdcall; external 'c:\tux\wtuxws32.dll' name 'tpstrerror';
function tuxputenv(ptr: pchar): integer; stdcall; external 'c:\tux\wtuxws32.dll' name 'tuxputenv';
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin

{***"wtuxws32.dll"TUXEDO的DLL,Delphi调用其函数,本段可以包含在公共的模块里面***}


begin
tuxputenv(pchar('WSNADDR=' + '//100.155.19.55:5120')); //设置中间件服务器地址

try
i:=tpinit(0); // 初始化(必须)
except
Tpterm; //异常结束
end;
strSerName:='SQLSERV_S_DS';

strParam:='select * from res.t_rs_gsmnum_a where svcnum=''AA''' ;
bufferType := 'STRING'; //调用的是STRING格式在CLIENT和SERVER传送数据
pChServiceName := stralloc(80); // 用于存放SERVICE名(不需要修改)
strpcopy(pChServiceName, bufferType); // 类型转化
sendLentgh := 1024 * 1024; //缓冲区大小可修改
//getLength := 0;
sendChaine := tpalloc(pChServiceName, nil, sendLentgh); // 向中间层申请的缓冲区,发送
getChaine := tpalloc(pChServiceName, nil,0); // 向中间层申请的缓冲区,接收
strpcopy(sendChaine, strParam); // 类型转化
strpCopy(pChServiceName, strSerName); // 类型转化

// 参数内容:发送的格式,放回的串类容,0,接收地址,返回的长度,0;
LIntRecv := tpcall(pChServiceName, sendChaine, 0, @getChaine, @tmplg, 0); //函数调用(反馈错误为912)
...全文
171 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ztenv 2009-03-26
  • 打赏
  • 举报
回复
代码太乱,没有办法看
javacaspnet 2009-03-26
  • 打赏
  • 举报
回复
帮顶,接分
gisyellow 2009-03-25
  • 打赏
  • 举报
回复
好久没用Delphi了,看着begin。。end觉得好亲切。。
zhoupq 2009-03-25
  • 打赏
  • 举报
回复
i= tpinit(0); // 初始化(必须)
初始化时i=1成功,问题好像在分配内存上面,不知如何处理?
zhoupq 2009-03-25
  • 打赏
  • 举报
回复
转换为C#源码:
public partial class Form1 : Form
{
[DllImport("c:\\tux\\wtuxws32.dll", SetLastError =true , EntryPoint = "tpinit")]
public static extern int tpinit(int tpinfo);
[DllImport("c:\\tux\\wtuxws32.dll", SetLastError = true, EntryPoint = "tpterm")]
public static extern int tpterm();
[DllImport("c:tux\\wtuxws32.dll", SetLastError = true, EntryPoint = "tpfree")]
public static extern int tpfree(string ptr);
[DllImport("c:\\tux\\wtuxws32.dll", SetLastError = true, EntryPoint = "tpalloc")]
public static extern IntPtr tpalloc(IntPtr types, IntPtr subtype, Int32 size);
[DllImport("c:\\tux\\wtuxws32.dll", SetLastError = true, EntryPoint = "tpcall")]
public static extern int tpcall(IntPtr svc, IntPtr idata, int ilen, out IntPtr odata, out int olen, int flags);
[DllImport("c:\\tux\\wtuxws32.dll", SetLastError = true, EntryPoint = "gettperrno")]
public static extern int gettperrn();
[DllImport("c:\\tux\\wtuxws32.dll", SetLastError = true, EntryPoint = "tpstrerror")]
public static extern string tpstrerror(int errno);
[DllImport("c:\\tux\\wtuxws32.dll", SetLastError = true, EntryPoint = "tuxputenv")]
public static extern int tuxputenv(IntPtr ptr);

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
string vstr;
int LIntRecv; //tp执行情况
IntPtr sendChaine;
IntPtr getChaine;
Int32 sendLentgh;
int TmpLg=0;
IntPtr pChServiceName;
string bufferType;
string strSerName, strParam;
string strResult;
tuxputenv(Marshal.StringToCoTaskMemAnsi("WSNADDR=" + "//100.155.19.55:5120")); //设置中间件服务器地址Marshal.StringToCoTaskMemAnsi(
try
{//Marshal.StringToCoTaskMemAuto
int i = 0;
i= tpinit(0); // 初始化(必须)
}
catch (Exception)
{
tpterm(); //异常结束
}
strSerName = "SQLSERV_S_DS";
bufferType= "STRING"; //调用的是STRING格式在CLIENT和SERVER传送数据
pChServiceName = Marshal.AllocCoTaskMem(80); // 用于存放SERVICE名(不需要修改)
pChServiceName = Marshal.StringToCoTaskMemAnsi(bufferType);
// pChServiceName = strSerName;

strParam = "select * from res.t_rs_gsmnum_a where svcnum='aa'";
sendLentgh = 1024*1024; //缓冲区大小可修改
//getLength := 0;
sendChaine = tpalloc(pChServiceName, IntPtr.Zero,sendLentgh); // 向中间层申请的缓冲区,发送
getChaine = tpalloc(pChServiceName, IntPtr.Zero, 0); // 向中间层申请的缓冲区,接收

LIntRecv = tpcall(pChServiceName,sendChaine , 0, out getChaine , out TmpLg, 0); //函数调用
string q=Marshal.PtrToStringAnsi(getChaine);

出示时成功,但LIntRecv=-1,Tmplg=912
,高手们瞅瞅!

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧