問合せを DLL に

  新規プロジェクト Hanbai



  
1) Win32 Dynamic-Link Library を選択
2) シンボルをエクスポートする DLL
3) ビルド -> アクティブな構成の設定で、Win32 Release を選択
4) 挿入 -> リソース -> ダイアログを選択 -> 新規作成
5) ファイル -> 上書き保存 -> Hanbai プロジェクトに移動 -> Hanbai.rc として保存
6) FileView タブ -> Source Files を選択 -> 右クリック -> ファイルをフォルダへ追加 -> Hanbai.rc を選択
7) Header Files を選択 -> 右クリック -> ファイルをフォルダへ追加 -> resource.h を選択
8) HM040 プロジェクトより IDD_DIALOG2 をコピー
9) プロジェクト -> 設定 -> C/C++タプ -> カテコリを「コード生成」 -> 呼び出し規約を __stdcall
10) プロジェクト -> 設定 -> リンク -> カテコリを「インプット」 -> 無視するライブラリに LIBCMT.lib
11) カテコリを「一般」 -> 出力ファイル名に C:\WINDOWS\system32\Hanbai.dll
  



  DlgSyain クラスを作成



DlgSyain.h

  
#include <CLClass.h>
#include <LboxDatabase.h>

class DlgSyain : public CLDlg  
{
public:
	void LVDblclick();
	void WMAll();
	void GetData();
	void ProcOperator();
	void ProcEnd();
	void WMInitdialog();

	DlgSyain();
	virtual ~DlgSyain();

	LboxDatabase CurDb;	// データベースオブジェクト(実体型)
	LboxListview *LView;	// リストビューオブジェクト(ポインタ型)

	LboxString Key;
	LboxString Name;
};
  

DlgSyain.cpp

※ 基本的に HM040 プロジェクトの MyDlg2.cpp と同じです

  
#include "stdafx.h"
#include "resource.h"
#include "DlgSyain.h"

//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////

DlgSyain::DlgSyain()
{

}

DlgSyain::~DlgSyain()
{

}

// *********************************************************
// ダイアログ初期化イベント
// *********************************************************
void DlgSyain::WMInitdialog()
{
	this->CenterWindow();

	// ダイアログのプロパティでシステムメニューのチェックボックスを外して下さい
	this->ChangeStyle( WS_MINIMIZEBOX | WS_SYSMENU, 0 );
	this->ChangeExStyle( WS_EX_APPWINDOW, 0 );
	this->StatusCreate( 0 );

	LboxString Buff;

	// MySQL 接続文字列
	Buff.operator = ("MySQL,lightbox,root,");
	CurDb.SetConnectString( 3, &Buff );

	// リストビューインスタンス作成
	LView = new LboxListview( ::GetDlgItem( hDlg, IDC_LIST1 ), 0 );
	LView->Grid();

	GetData();

	this->MoveWindow( 0, 0 );
}

// *********************************************************
// ダイアログ終了処理
// *********************************************************
void DlgSyain::ProcEnd()
{
	delete this->LView;
}

// *********************************************************
// オペレータイベント
// *********************************************************
void DlgSyain::ProcOperator()
{
	switch( ControlId ) {
		case IDOK:
			GetData();
			break;
	}
}

// *********************************************************
// 問合せ処理
// *********************************************************
void DlgSyain::GetData()
{
	if ( !CurDb.Connect() ) {
		MsgOk("接続に失敗しました \n%s", CurDb.ErrMessage.szLboxString);
		return;
	}

	LboxString Query;

	Query.operator = ("select * from 社員マスタ");

	LboxString Cond( "" );
	LboxString Buff;

	this->EditGetText( IDC_EDIT1, &Buff );
	Buff.Trim( "  " );
	if ( Buff.operator != ( ""  ) ) {
		if ( Cond.operator == ( "" ) ) {
			Cond.operator += (" where ");
		}
		else {
			Cond.operator += (" and ");
		}
		Cond.operator += (" 氏名 like ");
		Buff.Enclose( "%" );
		Buff.Enclose( "'" );
		Cond.operator += ( &Buff );
	}

	Query.operator += ( &Cond );

	int nRet;

	LView->Hide();
	this->StatusSetText( "" );
	nRet = CurDb.LoadSqlData( LView, 0, &Query );
	switch( nRet ) {
		case -1:
			this->StatusSetText( &(CurDb.ErrMessage) );
			break;
		case 0:
			this->StatusSetText( "対象データが存在しません" );
			break;
		default:
			this->StatusPrintf( "%d 件のデータが選択されました", nRet );
			break;
	}
	LView->Fit();
	LView->Show();

	CurDb.DisConnect();	
}

// *********************************************************
// 初期化とオペレータイベント以外の処理
// *********************************************************
void DlgSyain::WMAll()
{
	USE_LVIEW(LView)
}

// *********************************************************
// ダブルクリックイベント
// *********************************************************
void DlgSyain::LVDblclick()
{
	LboxString LWork;

	if ( LView->IsHandle( this->hTargetWindow ) ) {
		LView->GetColumnText( 0, &Key );
		LView->GetColumnText( 1, &Name );
		this->ProcEnd();
		::EndDialog( this->hDlg, IDOK );
	}
}
  



  呼び出し用 HrefSyain 関数を作成

Hanbai.def を作成してプロジェクトに追加
  
LIBRARY	"Hanbai.dll"

EXPORTS
	HrefSyain	@1
  

Hanbai.cpp
  
#include "stdafx.h"
#include "resource.h"
#include "Hanbai.h"
#include "DlgSyain.h"

DlgSyain objSyain;
DEFINE_DLG(objSyain)
HINSTANCE hInstance;

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
			hInstance = (HINSTANCE)hModule;
			CoInitialize(NULL);

			INIT_DLG(objSyain)

			INITCOMMONCONTROLSEX IC;
			IC.dwSize = sizeof( INITCOMMONCONTROLSEX );
			IC.dwICC = ICC_DATE_CLASSES;
			InitCommonControlsEx( &IC );
			break;
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
			break;
		case DLL_PROCESS_DETACH:
			CoUninitialize( );
			break;
    }
    return TRUE;
}


// これはエクスポートされた変数の例です。
HANBAI_API int nHanbai=0;

// これはエクスポートされた関数の例です。
HANBAI_API int fnHanbai(void)
{
	return 42;
}

HANBAI_API int HrefSyain( HWND hOwner, LPTSTR Key, LPTSTR Name )
{
	int ret;
	ret = DialogBox(
		hInstance, 
		(LPCTSTR)IDD_DIALOG2,
		hOwner,
		objSyain.CurDlgProc
	);
	lstrcpy(Key,objSyain.Key.szLboxString);
	lstrcpy(Name,objSyain.Name.szLboxString);
	return ret;
}

// これはエクスポートされたクラスのコンストラクタです。
// クラスの定義については Hanbai.h を参照してください。
CHanbai::CHanbai()
{ 
	return; 
}
  



  呼び出し側 ( HM040 プロジェクト )

呼び出し用関数ポインタの定義
  
typedef int (__stdcall *LPFUNC_1)
(
	HWND hOwner,
	LboxString *Kay,
	LboxString *Name
);
  

MyDlg.cpp のProcOperator

  
case IDM_REF:
//	Dlg2.Open( this, IDD_DIALOG2 );
	{
		HINSTANCE lib;

		// *******************************************
		// DLL ロード
		// *******************************************
		lib = LoadLibrary( "Hanbai.dll" );
		if ( lib == NULL ) {
			MsgOk( "%s のロードに失敗しました", "Hanbai.dll" );
			break;
		}

		LPFUNC_1 HrefSyain;

		// *******************************************
		// 関数アドレスのロード
		// *******************************************
		HrefSyain = (LPFUNC_1)GetProcAddress( lib, "HrefSyain" );
		if ( HrefSyain == NULL ) {
			FreeLibrary( lib );
			MsgOk( "アドレスの取得に失敗しました" );
			break;
		}

		char Key[128];
		char Name[512];
		LboxString LWork;
		int ret;
		ret = HrefSyain( this->hWnd, Key, Name );
		if ( ret != IDCANCEL ) {
			this->EditSetText( IDC_KEY, Key );
			this->Type->SelectedGetData( &LWork );
			if ( LWork.operator == ("1") ) {
				this->Type->Select( 1 );
			}
			this->PostCommand( IDC_CHECK );
		}
		
		// *******************************************
		// DLL 解放
		// *******************************************
		FreeLibrary( lib );
	}
	break;

  



  VB で呼び出す場合

  
Public Declare Function HrefSyain Lib "Hanbai" _
( _
    ByVal hWnd As Long, ByVal lpKey As String, ByVal lpName As String _
) As Long
  

  
Dim strKey As String
Dim strName As String

strKey = String(512, Chr(0))
strName = String(512, Chr(0))

ret = HrefSyain(Me.hWnd, strKey, strName)

sKey = Left(strKey, InStr(strKey, Chr(0)) - 1)
sName = Left(strName, InStr(strName, Chr(0)) - 1)

MsgBox (ret & "|" & sKey & "|" & sName)
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ