我用VC调用DLL中的函数,出现"内存不能为written"的问题

JavaNoob 2008-03-05 12:03:59
具体是这样的,现在手头上有一个Fnthex32.dll,以下是函数说明,有点乱:
* Function in FNTHEX.DLL:
* GETFONTHEX() - Get bitmap in hex code of specific out
* string and font to buffer.
* Parameter:
* 1) LPSTR outStr, // output string
* 2) LPSTR lfFaceName, // Windows font name
* 3) LPSTR outstrname // output string name
* 4) short int lfOrientation, // clock-wish Orientation: 0,90,180,270
* 5) short int lfHeight, // font height
* 6) short int lfWidth, // font width, always set to 0
* 7) short int lfBold, // bold font style
* 8) short int lfItalic, // italic font style
* 9) LPSTR hexBuf // buffer to receive hex codes,
* size must set to 21K.
* Return : Byte count of buffer contents if successful, otherwise <= 0
*
* Note : 1) Before program to call function GETFONTHEX() in FNTHEX32.DLL,
* Statement must be added to declare it in the call program.
* 2) Function name GETFONTHEX() must in upper case.
* 3) Before function GETFONTHEX() is called, the buffer that is equal
* to 21K must to allocate first.
* 4) The return of GETFONTHEX() is greate than 0 if function call is
* successful, and result of Chinese data is stored in 21K buffer.
* The total number of byte output in buffer is return by GETNFONTHEX().
* 5) Printer driver "Generic / Text Only" must be set for
* label printing under Windows 32 bit environment.

我不确知道以上信息是C/C++还是其他语言的,关于调用Fnthex32中的GETFONTHEX函数网上很多VB/DELPHI方法,但我需要的是C/C++,因为我主要是用JAVA通过C++去使用这个DLL,以下是我的一个测试用的C程序代码:
#include "stdlib.h"
#include "string.h"
#include "windows.h"
#include "stdio.h"
#include "iostream.h"


int main(){




typedef int (_stdcall* FNC1)(LPSTR,LPSTR,LPSTR,short int,short int,short int,short int,short int,LPSTR);

HINSTANCE hDLL=::LoadLibrary("d:\\BIN\\Fnthex32.dll");
if(hDLL!=NULL){
FNC1 fun=(FNC1)::GetProcAddress(hDLL,"GETFONTHEX");
if(fun==0){
printf("no such function found.\n");
FreeLibrary(hDLL);
}else
{

int i;
i=125;
//char* buf = new char[21504];
//LPSTR buf = "";
LPSTR buf = (LPSTR)malloc(21*1024);

//i = fun("中文","宋体","chnstr01",0,50,0,1,0,buf);
cout<<&buf<<endl;

FreeLibrary(hDLL);

}
}


return 0;
}

如果不把"i = fun("中文","宋体","chnstr01",0,50,0,1,0,buf);"这行注释掉就会提示内存不能WRITTEN之类的,怎么解决呢?谢谢大家.
...全文
447 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
JavaNoob 2008-03-11
  • 打赏
  • 举报
回复
问题已经解决,被函数说明骗了,多了个参数。
现在可以直接通过JAVA使用这个DLL(其实还是要用JNI,不过有个组件封装好了)。

虽然用了很多时间,但让我学了很多东西,知道怎么解决JNI编码问题,还有JAVA中向本地方法传递类似C/C++里的参数的办法。
lala_benben 2008-03-09
  • 打赏
  • 举报
回复
....LZ辛苦了..JAVA没这么多讲究..
野男孩 2008-03-08
  • 打赏
  • 举报
回复
LPSTR相当于char*。

"中文"则是const chr*。

可能你调用的那个函数会要修改你传入的参数,而你传入的是const char*指向的内存是ReadOnly的。。。。
  • 打赏
  • 举报
回复
LPSTR,LPSTR,LPSTR
这些参数的话,你就不能将,"中文"这样的参数直接写到参数列表里面
需要用
char chs[32]= "中文"
fun( chs , ....);这样的调用

70,032

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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