delphi 如何转换数据给VC6 中CString(SOCKET通讯) ( 积分: 50 )

L

linkdev

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个SOCKET通讯函数写在DLL中,是用VC6写的,现在想用DELPHI调用它,并发送数据,
VC6 中CString 写在DLL函数参数中,DELPHI调用此DLL中函数时可用哪种类型转换数据给CSTRING,我目前测试用PCHAR ,每次只有第一个字符可被DLL函数接受,调用还出内存错误,搞得头痛? 不知有无人写过类似delphi程序
以下是该DLL的VC源码:
#include "stdafx.h"
#include "GWJ_LED.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//

/////////////////////////////////////////////////////////////////////////////
// CGWJ_LEDApp
SOCKET m_hSocket;
char m_szRemoteIP[20];
int m_RemotePort;
sockaddr_in m_addr;
sockaddr m_add;
BEGIN_MESSAGE_MAP(CGWJ_LEDApp, CWinApp)
//{{AFX_MSG_MAP(CGWJ_LEDApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGWJ_LEDApp construction

CGWJ_LEDApp::CGWJ_LEDApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CGWJ_LEDApp object

CGWJ_LEDApp theApp;
extern "C" _declspec(dllexport) int LED_Write3(CString addess,int port,CString plan,CString real,CString per)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

char szSend[100];
CString tmp;
tmp="3a00000c";
if(plan.GetLength()<5)//&amp;sup3;¤&amp;para;&amp;Egrave;&amp;sup2;&amp;raquo;&amp;sup1;&amp;raquo;&amp;Ccedil;°&amp;frac14;&amp;Oacute;&amp;iquest;&amp;Otilde;&amp;cedil;&amp;ntilde;
{
for(int i=1;i<(6-plan.GetLength());i++)
tmp=tmp+&quot; &quot;;
}
tmp=tmp+plan;

if(real.GetLength()<5)
{
for(int i=1;i<(6-real.GetLength());i++)
tmp=tmp+&quot; &quot;;
}
tmp=tmp+real;
if(per.GetLength()<4)
{
for(int i=1;i<(5-per.GetLength());i++)
tmp=tmp+&quot; &quot;;
}
tmp=tmp+per;
tmp=tmp+&quot;a&quot;;
strcpy(szSend,LPCTSTR(tmp));
strcpy(m_szRemoteIP,LPCTSTR(addess));
int ret;

SOCKADDR_IN addr;
WSADATA wsData;
WORD VerRequested=MAKEWORD(2,2);
WSAStartup(VerRequested,&amp;wsData);

if(m_hSocket !=NULL)
{
closesocket(m_hSocket);
m_hSocket=NULL;
}
if(m_hSocket ==NULL)
{
//m_hSocket=socket(PF_INET,SOCK_STREAM,0);//TCPIP
m_hSocket=socket(AF_INET,SOCK_DGRAM,0);//UDP
ASSERT(m_hSocket !=NULL);
}
if(m_hSocket==INVALID_SOCKET)
{
return -1;
}
ZeroMemory(&amp;addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(m_szRemoteIP);
addr.sin_port = htons(port);
ret = sendto(m_hSocket,szSend,24,0,(sockaddr *)&amp;addr,sizeof(addr));
return ret;
}
 
我的问题是如何在DELPHI中调用此函数
LED_Write3(CString addess,int port,CString plan,CString real,CString per)
 
内存出错是因为你没在项目中引入sharemem这个系统单元吧,这个单元调用dll文件必须放在项目内,要不就是内存不能为读或写
 
有此引入sharemem这个系统单元
问题重点是如何转换一个string或int类型到cstring类型?
 
delphi的dll文件字符是pchar类型pchar等同于string
其他int在delphi中是integer
我觉得调用这个函数直接string就形,我采用静态调用的时候就是直接string传递
 
天浪影,
请注意是MFC的Cstring类型
他在delphi中不能直接被调用
没有你说的那么容易 不然早解决了
 
这个我就不太清楚了
我记得dll文件是在内存中交换数据,在内存中string类型是一个,语言通用字符类型是pchar.MFC编程没做个,c++里的MFC类型还有变化,这个不太清楚,我调用过c++写的dll文件没遇到过.我来关注中.学习一下
 
顶部