2

I'm working on a graphical interface written in VB6, where I have to call function contained in a certain DLL written in C. Because of a known limitation I had to implement a trick that allows me to load this DLL in a implicit way.

This is possible creating an IDL file, compile it with MIDL and reference the resulting .tlb file in the VB6 project.

The problem is that VB6 strings and C arrays of char do not match, so I can't pass (and get back) them to the DLL.

The prototype of the C function is:

int __stdcall myFunc(char filename_in[], char filename_out[], char ErrMsg[]);

What should I write in the IDL file and how should I call it from VB6?

Thanks.

4

3 に答える 3

3

VB6 互換の文字列を使用するには、BSTR を使用する必要があります。これは標準の COM 文字列型であり、Win32 API と同様に、Unicode 文字列を utf-16 エンコーディングで格納します。

 int __stdcall myFunc(BSTR filename_in, BSTR filename_out, BSTR* ErrMsg);

in args を WCHAR* に直接キャストできます。char* に変換する必要がある場合は、WideCharToMultiByte() を使用します (避けるのが最善です)。null でない場合は SysFreeString を使用し*ErrMsgて、割り当てる前に既存の文字列を解放します。SysAllocString を使用して、ErrMsg 文字列を割り当てます。これも utf-16 文字列である必要があり、必要に応じて MultiByteToWideChar() を使用して char* から変換します。または、L"Oops" のように、L で始まる文字列リテラルを使用します。

于 2011-03-08T14:15:24.347 に答える
2

VB6 では、ANSI 文字列パラメーターを使用して stdcall 関数を問題なく使用できます。IDL で使用するだけ[in] LPSTR filename_inで、ランタイムが UNICODE<->ANSI 変換を自動的に行います。

「魔法」は[out]パラメーターにも機能します。

于 2011-03-08T14:15:08.980 に答える
0

GSergwqwのおかげで、この問題の解決策が見つかりました。

IDL ファイルでは、char 配列を LPSTR として宣言する必要があるため、関数のプロトタイプは次のようになります。

int _stdcall myFunc(LPSTR file_name_in, LPSTR file_name_out, LPSTR ErrMsg)

出力メッセージが含まれている場合でも、他の配列とまったく同じように宣言されていることに注意してErrMsgください (VB6 側で読み取り可能)。

VB6 側では、文字列を次のように割り当てる必要があります。

Dim file_name_in As String * 256
Dim file_name_out As String * 256
Dim ErrMsg As String * 256

そうすることで、これらの文字列は の限られたサイズで割り当てられるため256、C DLL の char 配列と互換性があります。

これが他の誰かを助けることを願っています。

よろしく、

GB

于 2011-03-08T14:44:30.980 に答える