Используйте Type Library, тогда вызовы будут быстрее и удобнее.
Примеры объявлений (создаём в Visual C++ или в блокноте файл mem4.id):
- Код: Выделить всё
[
uuid(30A98234-7CB6-44c8-94CA-D166BAE71234),
version(1.0),
helpstring("Using GetMem4")
]
library GetMem4
{ importlib("stdole2.tlb");
[
dllname("msvbvm60"),
helpstring("GetMem4 Functions")
]
module msvbvm60 {
[entry("GetMem4"), propget,helpstring("Returns or sets the value referenced by the source parameter cast to a DWord Long.")]
HRESULT __stdcall AsLong(
[in] void * Ptr,
[out, retval] long * lpRetVal
);
[entry("PutMem4"), propput,]
HRESULT __stdcall AsLong(
[in] void * Ptr,
[in] long NewValue
);
[entry("GetMem4"),helpstring("Returns the argument cast to a DWord structure.")]
HRESULT __stdcall AsDWord([in] void * Ptr, [out, retval] DWord * lpRetVal);
[entry("GetMem4"), propget,helpstring("Sets or returns the 32-bit Long integer value at the specified memory address.")]
HRESULT __stdcall Mem(
[in] long Address,
[out, retval] long * lpRetVal
);
[entry("PutMem4"), propput,]
HRESULT __stdcall Mem(
[in] long Address,
[in] long NewValue
);
[entry("GetMem4"), propget,helpstring("Returns or sets the value of the source string's BSTR pointer. This property allows read/write access to the same value that is returned by Visual Basic's (hidden) StrPtr function.")]
HRESULT __stdcall StringPtr(
[in] BSTR * Source,
[out, retval] long * lpRetVal
);
[entry("PutMem4"), propput,]
HRESULT __stdcall StringPtr(
[in] BSTR * Source,
[in] long NewValue
);
[entry("GetMem4"), propget,helpstring("Returns or sets the value of an array's SAFEARRAY descriptor pointer. This property allows read/write access to the value referenced by the pointer returned by ArrPtr. Use StrSAPtr for string arrays to prevent the implicit ANSI/Unicode conversion.")]
HRESULT __stdcall SAPtr(
[in] SAFEARRAY(void) * Array,
[out, retval] long * lpRetVal
);
[entry("PutMem4"), propput,]
HRESULT __stdcall SAPtr(
[in] SAFEARRAY(void) * Array,
[in] long NewValue
);
[entry("GetMem4"), propget,helpstring("Returns or sets the value of the SAFEARRAY descriptor pointer of a string array. This property allows read/write access to the value referenced by the pointer returned by StrArrPtr.")]
HRESULT __stdcall StrSAPtr(
[in] SAFEARRAY(BSTR) * Array,
[out, retval] long * lpRetVal
);
[entry("PutMem4"), propput,]
HRESULT __stdcall StrSAPtr(
[in] SAFEARRAY(BSTR) * Array,
[in] long NewValue
);
[entry("GetMem4"),propget,helpstring("Expects a ByRef object argument and returns or sets the value of the object pointer. This property allows read/write access to the same value that is returned by ObjPtr.")]
HRESULT __stdcall ObjectPtr(
[in] void * Object, // IUnknown **
[out, retval] long * lpRetVal
);
[entry("PutMem4"),propput,]
HRESULT __stdcall ObjectPtr(
[in] void * Object, // IUnknown **
[in] long NewValue
);
[entry("GetMem4"),propget,helpstring("Returns or sets the value of the source object's VTable interface pointer. This property allows read/write access to the value referenced by the pointer returned by both ObjPtr and ObjectPtr. Should not be used on objects equal to Nothing.")]
HRESULT __stdcall VTablePtr(
[in] IUnknown * Object,
[out, retval] long * Value
);
[entry("PutMem4"),propput,]
HRESULT __stdcall VTablePtr(
[in] IUnknown * Object,
[in] long NewValue
);
[entry("GetMem4"),helpstring("Assigns a 32-bit dword value from the source to the destination parameter's memory location.")]
HRESULT __stdcall AssignDWord(
[in] long Source,
[in, out] void * Destination
);
};
};
Для компиляции введите в командоной строке или пропишите в .BAT-файле:
- Код: Выделить всё
cd C:\Program Files\Microsoft Visual Studio\VC98\Bin\
MIDL.exe mem4.idl
Добавьте полученный TLB-файл в VB (меню Project -> References -> Browse..) *
Доступные методы можно посмотреть по F2.
* Перед использованием Type Library, импортирующей msvbvm60.dll, установите Visual Studio Service Pack 6.
Ваш пример с использованием Type Library:
- Код: Выделить всё
Option Explicit
Sub Main()
Dim myvar As Long
myvar = 1234
CopyMyVar VarPtr(myvar)
End Sub
Public Sub CopyMyVar(ByVal VarAddr As Long)
MsgBox Mem(VarAddr) 'показывает 1234
End Sub