DELPHI调用VB的DLL出错:"Access violation ......",请问是怎么回事?
解决方案 »
- Delphi7 中使用JSON.pas
- 【菜鸟问题】如何删除控件
- 更新修改表
- 用ado连接数据库是 怎么不让他出现输入密码
- 连接数据库,有源码! 在线等
- 请问如何编程实现在“快速启动”栏添加应用程序的快捷方式?
- 问一个简单的问题--如何获取应用程序的图标
- 怎样用一个程序,往oicq发送消息框,添加信息?用ole么?
- 请问各位老大,哪里有indy控件的下载?或告诉几个比较多的组件的地方
- 怎样在richedit中使用鼠标的滚轮呢?
- 在这里下载个fastreport,一打开demo就说false不是有效integer值
- 请问如何用ADOConnection、ADOQuery控件得到字段的数据类型、长度?
Value1 As Double Value2 As Integer
Value3 As String * 20
Value4 As String
End Type
A: You will have some trouble with Value4. Passing VB strings to/from DLLs takes special handling. The easiest way to do it is to pass a pre-declared VB string to the DLL and have the DLL return the length and the resulting string back.The others are easy. Value1 As Double -> Value1 : Longint;
Value2 AS Integer -> Value2 : integer;
Value3 As String * 20 -> Value3 : array[0..19] of char;User defined structures in Pascal are called records. type
myStructure = record
Value1 : longint;
Value2 : integer;
Value3 : array [0..19] of char;
end;I think you could define Value4 as a PChar in Pascal, but when you got it back in VB, you would have to search it for the ASCII 0 end byte and change the length of your string to the number of characters before the zero.If I remeber correctly, from my evaluation 'beta' copy with source code, there were some functions in the VCL that handle VB strings. Can't say if they are in the shipping version because I havn't recieved the VCL source yet. A quick search of the online docs don't reveal them.Here's some general type conversions from VB to Pascal (in table form; I hope this formats correctly on CIS.): VB Declare As VB Call With Translates to Pascal Type
------------- ------------ -------------------------
By Val S As String Any String or Variant PChar
I As Integer Any Integer ^Integer (pointer to integer)
L As Long Any Long ^Longint (pointer to longint) S As Rect Any Variable of same type ^TRect (pointer to TRect)
By Val As Integer Any Integer BOOL (word boolean)
" " " Word
" " " Integer
" " " hWnd
" " " hDC
" " " ...and so on; all word types.
By Val As Long Any Long Longint I As Integer the first element of I(0) ^array of integer
As Any Any Variable (By Val when
String) Pointer (PChar when string)
As Any By Val 0& nil
Q: This would probably work, but I'd still really like to know how to return a string (even a null-terminated one) from a DLL function to VB.A: The problem with returning PChars from DLLs is that the DLL has to be responsible for cleaning up the memory, but it doesn't know when the caller finishes. Most people use the following style, which is NOT bullet proof, but works in most instances: Var ReturnBuffer :Array [0..255] of Char; { Must be outside function! } function Dir_Get(ACaption, AInitDir, DirText: PChar ) : PChar;
Begin
...
Result := StrPCopy(ReturnBuffer, 'Result Text');
End;The only time you have a problem is when someone calls the function while someone else is still using the ReturnBuffer. (Just about guaranteed not to happen, except maybe under Windows 95, but could under 3.1 if VB doesn't copy the string).