天野と申します。VB.net初心者で
いつも勉強させていただいてます。
早速質問をさせてください。
VC++6.0で作成したDLLをVB.net(2005)から呼び出しています。
//------------------------------------------------------------------------------
DLLの関数は、構造体を含む構造体を引数に持っていて、
関数の機能としては内部でスレッドを起動しその引数に値をセットしています。
typedef struct{
HWND hw;
HANDLE hTh;
:
}tag_NESTA;
typedef struct{
BYTE byA;
BYTE byB;
:
}tag_NESTB;
typedef struct{
tag_NESTA s_NESTA;
tag_NESTB s_NESTB;
}tag_PARAM;
#ifdef __cplusplus
extern "C"
{
#endif
// FuncA 内部でスレッドを使用しない
extern "C" DLL_API int _stdcall FuncA ( tag_PARAM* s_PARAM );
// FuncB 内部でスレッドを使用する
extern "C" DLL_API int _stdcall FuncB ( tag_PARAM* s_PARAM );
#ifdef __cplusplus
}
#endif
//------------------------------------------------------------------------------
VB.netから呼び出したとき
スレッドを起動しないFuncAは、正常に引数の構造体に値がセットされているのですが、
スレッドを起動するFuncBの時は、VB.netからみるとその引数に値が入っていません。
VB.netでは以下のように作成しています。
Imports System.Runtime.InteropServices
Class DLLRAP
<StructLayout(LayoutKind.Sequential)> _
Public Structure tag_NESTA
Public hw As System.IntPtr
Public hTh As System.IntPtr
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure tag_NESTB
Public byA As Byte
Public byB As Byte
:
End Structure
<StructLayout(LayoutKind.Sequential)> _
Public Structure tag_PARAM
Public s_NESTA As tag_NESTA
Public s_NESTB As tag_NESTB
End Structure
<DllImport(DLL_NAME)> _
Public Shared Function FuncA _
(ByRef s_PARAM As tag_PARAM) As Integer
End Function
<DllImport(DLL_NAME)> _
Public Shared Function FuncB _
(ByRef s_PARAM As tag_PARAM) As Integer
End Function
End Class
VB.net呼出元
Public Class Form
'この画面で使用する構造体の定義
Shared s_PARAM As New RFIDRAP.tag_PARAM()
'DLL関数呼出
Private Sub btnA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
s_PARAM.s_NESTA.hTh = Nothing
s_PARAM.s_NESTA.hw = Me.Handle
DLLRAP.FuncA(s_PARAM)
s_PARAM.s_NESTB.byA '←値が入っている
s_PARAM.s_NESTB.byB '←値が入っている
END SUB
'DLL関数呼出
Private Sub btnB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
s_PARAM.s_NESTA.hTh = Nothing
s_PARAM.s_NESTA.hw = Me.Handle
DLLRAP.FuncB(s_PARAM)
s_PARAM.s_NESTB.byA '←値が入っていない
s_PARAM.s_NESTB.byB '←値が入っていない
END SUB
END CLASS
といった状態です。なおスレッドの機能自体は正常に動作しているようです。
スレッドの時に気をつけないといけないことなどあるでしょうか。
VCもVBもあまり経験がない為、些細なことでもご教授いただけると幸いです。
よろしくお願いいたします。
"同期・非同期"や"Threading"についてMSDNを調べてみるとか。
そもそも別スレッドであるということは、時系列を共有していない、
ということになることを意識しないと。
ツイート | ![]() |