vb调用DLL时,有VB6.EXE (Cipher.dll) 处有未经处理的异常:读取位置 0x00000000 时发生访问冲突

qq_41716731 2018-05-08 09:24:12
用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=wireless_buf[j]*0x100+wireless_buf[j+1];
if((compress_data&0x8000)!=0)
compress_data=((compress_data/0x100)^0x89)*0x100+compress_data%0x100;
for(k=0;k<8;k++){
compress_data<<=1;
if((compress_data&0x8000)!=0)
compress_data=((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=((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


...全文
981 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sw519 2018-05-25
  • 打赏
  • 举报
回复
把vc的接口函数的所有string改成char*试试
赵4老师 2018-05-10
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
舉杯邀明月 2018-05-09
  • 打赏
  • 举报
回复
感觉你在 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控件。

7,764

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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