用cpp寫的DLL,在cpp呼叫時沒有問題,但是在vb中呼叫時報錯:VB6.EXE 中的 0x66c36314 (Cipher.dll) 處有未經處理的例外: 0xC0000005: 讀取位置 0x00000000 時發生訪問沖突
小弟才疏學淺,實在搞不明白了,請大家幫忙看看,感激不盡
Cipher.cpp
#include "stdafx.h"
#include "Cipher.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
__declspec(dllexport) string __stdcall compress_wireless(string str)
{ int j,k,g,resend_buf[MAX_PACK_SIZE],wireless_buf[MAX_PACK_SIZE];
static string array[MAX_PACK_SIZE],compress_data1,compress_data2;
unsigned int compress_data;
for(unsigned int i=0;i<=str.length();i+=3){
g =i/3;
array[g]=str.substr(i,2);}
for(int h=0;h<=14;h++){
stringstream ss_1;
ss_1<<hex<<array[h];
ss_1>>wireless_buf[h];
ss_1.str("");
}
for(j=0;j<15;j++)
resend_buf[1+j]=wireless_buf[j];
for(j=0;j<14;j++){
compress_data=https://bbs.csdn.net/topics/wireless_buf[j]*0x100+wireless_buf[j+1];
if((compress_data&0x8000)!=0)
compress_data=https://bbs.csdn.net/topics/((compress_data/0x100)^0x89)*0x100+compress_data%0x100;
for(k=0;k<8;k++){
compress_data<<=1;
if((compress_data&0x8000)!=0)
compress_data=https://bbs.csdn.net/topics/((compress_data/0x100)^0x89)*0x100+compress_data%0x100;
}
wireless_buf[j+1]=compress_data/0x100;
}
for(k=0;k<7;k++){
compress_data<<=1;
if((compress_data&0x8000)!=0)
compress_data=https://bbs.csdn.net/topics/((compress_data/0x100)^0x89)*0x100+compress_data%0x100;
}
compress_data<<=1;
stringstream ss_2;
ss_2<<hex<<compress_data;
ss_2>>compress_data1;
compress_data2 = compress_data1.substr(0,2);
transform(compress_data2.begin(),compress_data2.end(),compress_data2.begin(),towupper);
return compress_data2;
}
Cipher.h
#pragma once
#include "targetver.h"
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include<algorithm>
using namespace std;
#define WIN32_LEAN_AND_MEAN
#define MAX_PACK_SIZE 20
#ifdef CIPHER_EXPORTS
#define CIPHER_API __declspec(dllexport)
#else
#define CIPHER_API __declspec(dllimport)
#endif
// 此類是從 Cipher.dll 匯出的
__declspec(dllexport) string __stdcall compress_wireless(string str);
Cipher.def
LIBRARY Cipher
EXPORTS compress_wireless
VB呼叫程序
Option Explicit
Private Declare Function compress_wireless Lib "C:\Users\Administrator\Desktop\Cipher\Debug\Cipher.dll" (ByVal p As String) As String
Private Sub Command1_Click()
Dim str As String
str = compress_wireless("80 66 0A 99 0B 28 0A 0D 0D 0D 0D 0D 0D 0D 00 ")
Text1..Text = str
End Sub
Private Sub Text1_Change()
End Sub
uj5u.com熱心網友回復:
感覺你在 VB中的API函式型別宣告不正確。
從 Cipher.cpp 代碼第22行來看,我覺得API回傳的值是“字串首址指標”,
那么VB6中應該把函式回傳值宣告為 Long 型別。
然后,直接按回傳的指標值,直接Copy “MAX_PACK_SIZE”位元組的資料到一個Byte陣列中。
// 資料型別的定義:
static string array[MAX_PACK_SIZE],compress_data1,compress_data2;
// API的回傳:
return compress_data2;
如果這“回傳資料”都是有效的ASCII字符,
可以用Strconv( )函式把它從 Byte陣列轉換成字串,再賦值給 TextBox控制元件。
uj5u.com熱心網友回復:
崩潰的時候在彈出的對話框按相應按鈕進入除錯,按Alt+7鍵查看Call Stack即“呼叫堆疊”里面從上到下列出的對應從里層到外層的函式呼叫歷史。雙擊某一行可將游標定位到此次呼叫的源代碼或匯編指令處,看不懂時雙擊下一行,直到能看懂為止。uj5u.com熱心網友回復:
把vc的介面函式的所有string改成char*試試轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/46911.html
標籤:VB基礎類
上一篇:excle表中實作兩列資料關聯
下一篇:新手自學VB6,代碼出現問題請教
