VB6调用VC6生成的DLL文件中的函数,大神帮忙解决问题
刚接触VB和VC请大神指点:
我用VC6生成了DLL文件(摄像机抓拍用的),在VC6中调用测试通过(连接设备可以抓拍),但是拿到VB6中调用函数,调用函数给的参数和VC6中调用给的参数是一样的,但是函数调用了,可总是无法实现抓拍,不知道哪里出错了,求大神给看看:
VC6:
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <Windows.h>
#include "TFCNetSDK.h"
#include "WinCon.h"
#include <objbase.h>
#include <time.h>
#include <iostream>
#include <conio.h>
#define _WIN32_WINNT 0x0500
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow ();
extern "C" __declspec(dllexport) LONG _stdcall getlUserID(char *ip,char *name,char *password,int port);
extern "C" __declspec(dllexport) LONG _stdcall getlRealHandle(LONG lUserID);
extern "C" __declspec(dllexport) void _stdcall closelogin(LONG lUserID);
extern "C" __declspec(dllexport) char* _stdcall getphoto(LONG lRealHandle,char *url,char *jiqino);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
//用户登陆
LONG _stdcall getlUserID(char *ip,char *name,char *password,int port){
//初始化SDK
int a = TFC_NET_Init();
//登陆用户实体
UserLoginInfo_t UserLoginInfo;
//实体类赋值
strcpy(UserLoginInfo.userIPAddr.ipV4, ip);
strcpy(UserLoginInfo.szUserName, name);
strcpy(UserLoginInfo.szPassword, password);
//端口
UserLoginInfo.port = port;
//返回-1表示注册登陆失败
LONG lUserID = TFC_NET_Login(&UserLoginInfo, NULL);
if (lUserID == -1)
{
//查看原因
printf("Login to Device failed! Error : %d\n", TFC_NET_GetLastError());
//printf("Login to Device failed! Error : %s\n", TFC_NET_GetErrorMsg(errorNO));
//停止运行
return -1;
}
return lUserID;
}
//调视频画面
LONG _stdcall getlRealHandle(LONG lUserID){
HWND hWnd = GetConsoleWindow();
TFC_REALPLAY ClientInfo = { 0 };
ClientInfo.hPlayWnd = hWnd;
ClientInfo.lChannel = 1;
ClientInfo.lLinkMode = TSoverTCP;
ClientInfo.csMultiCastIP = NULL;
ClientInfo.bWithaudio = 1;
ClientInfo.lStreamidx = STREAM_IDX_VIDEO_MAIN;
ShowWindow(hWnd,SW_HIDE);
LONG lRealHandle = TFC_NET_Realplay(lUserID,&ClientInfo,NULL,NULL,1);
if (lRealHandle < 0)
{
printf ("Start Play failed! Error : %d\n", TFC_NET_GetLastError());
TFC_NET_Logout(lUserID);
TFC_NET_Cleanup();
return -1;
}
//画面出现需要一点时间
Sleep(3000);
return lRealHandle;
}
//注销
void _stdcall closelogin(LONG lUserID){
TFC_NET_Logout(lUserID);
TFC_NET_Cleanup();
}
//抓拍
char* _stdcall getphoto(LONG lRealHandle,char *url,char *jiqino){
LONG catiureJPEG = NULL;
char *timename=new char[100];
//for(int i=0;i<1;i++){
SYSTEMTIME sys;
GetLocalTime(&sys);
/*printf( "%M%d%d %d:%d:%d.%d 星期\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek);*/
char t[10];
strcpy(timename,url);
strcat(timename,itoa(sys.wYear,t,10));
strcat(timename,itoa(sys.wMonth,t,10));
strcat(timename,itoa(sys.wDay,t,10));
strcat(timename,itoa(sys.wHour,t,10));
strcat(timename,itoa(sys.wMinute,t,10));
strcat(timename,itoa(sys.wSecond,t,10));
strcat(timename,itoa(sys.wMilliseconds,t,10));
strcat(timename,".jpg");
//puts(timename);
const char *szFileName = timename;
catiureJPEG = TFC_NET_CaptureJPEG(lRealHandle,szFileName,NULL);
//}
return timename;
}
在VB6中调用上面VC6生成的dll:
Private Declare Function getlUserID Lib "Serverdll.dll" Alias "_getlUserID@16" (ByRef ip As String, ByRef name1 As String, ByRef Password As String, ByRef port As Integer) As Long
Dim ip As String
Dim name1 As String
Dim Password As String
Dim port As Integer
Dim lUserID As Long
Private Declare Function getlRealHandle Lib "Serverdll.dll" Alias "_getlRealHandle@4" (ByRef lUserID As Long) As Long
Dim lRealHandle As Long
Private Declare Function getphoto Lib "Serverdll.dll" Alias "_getphoto@12" (ByRef lRealHandle As Long, ByRef url As String, ByRef jiqino As String) As Byte
Dim url As String
Dim jiqinno As String
Dim picname As String
Private Declare Function closelogin Lib "Serverdll.dll" Alias "_closelogin@4" (ByRef lUserID As Long)
Private Sub Command1_Click()
MsgBox "测试开始"
ip = "192.168.0.120"
name1 = "admin"
Password = "admin"
port = 1115
lUserID = getlUserID(ip, name1, Password, port)
MsgBox lUserID
lRealHandle = getlRealHandle(lUserID)
MsgBox lRealHandle
url = "C:\\image\"
jiqinno = "001"
picname = getphoto(lRealHandle, url, jiqinno)
MsgBox picname
closelogin (lUserID)
MsgBox "测试结束"
End Sub
参数完全是搬过来的,但是就是无法抓拍 不好使啊...getlUserID(ip, name1, Password, port)方法总是返回个 -1(登陆失败) ,求大神解决
[解决办法]
如果用 As String 则要 ByVal。因为 VB 中的字符串已经是类似指针的结构。
也可以声明成 ByRef ... As Byte,传 Byte 数组。调用时:
lUserID = getlUserID(ip(0), name1(0), Password(0), port(0))
[解决办法]
你的原来的那个 getlUserID( ) 函数,传入的 ip、name、Password,在 VB6的字符串末尾都应该添加 vbNullChar 吧!
还有,Byval port As Integer ,应该是 as Long 才对吧?