開発環境 Microsoft Visual C++ 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 空の CLR プロジェクト
プロジェクト名 clrapi

参考

clrapi1.cpp
using namespace System;
using namespace System::Runtime::InteropServices;
 
// Managed class name
typedef IntPtr HWND;
typedef UInt32 UINT;
typedef String^ LPCTSTR;
/*/
// Unmanaged C language type
typedef void* HWND;
typedef unsigned int UINT;
typedef const wchar_t* LPCTSTR;
*/
[DllImport("user32", CharSet=CharSet::Auto)]
int MessageBox(
  HWND hWnd,          // オーナーウィンドウのハンドル
  LPCTSTR lpText,     // メッセージボックス内のテキスト
  LPCTSTR lpCaption,  // メッセージボックスのタイトル
  UINT uType          // メッセージボックスのスタイル
);
 
//==============================================================================
int main()
{
	String^ lpText = L"hello, world";
	String^ lpCaption = L"Platform Invoke Sample";
	MessageBox((HWND)0, lpText, lpCaption, 0);
//	MessageBox(0, L"hello, world", L"Platform Invoke Sample", 0);
	return 0;
}
 



clrapi2.cpp
using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;
 
typedef Int32 INT;
typedef UInt32 UINT;
typedef Int32 BOOL;
typedef UInt32 DWORD;
typedef StringBuilder^ LPTSTR;
typedef String^ LPCTSTR;
 
[DllImport("kernel32", CharSet=CharSet::Auto)]
UINT GetPrivateProfileInt(
  LPCTSTR lpAppName,  // セクション名
  LPCTSTR lpKeyName,  // キー名
  INT nDefault,       // キー名が見つからなかった場合に返すべき値
  LPCTSTR lpFileName  // .ini ファイルの名前
);
 
[DllImport("kernel32", CharSet=CharSet::Auto)]
DWORD GetPrivateProfileString(
  LPCTSTR lpAppName,        // セクション名
  LPCTSTR lpKeyName,        // キー名
  LPCTSTR lpDefault,        // 既定の文字列
  LPTSTR lpReturnedString,  // 情報が格納されるバッファ
  DWORD nSize,              // 情報バッファのサイズ
  LPCTSTR lpFileName        // .ini ファイルの名前
);
 
[DllImport("kernel32", CharSet=CharSet::Auto)]
BOOL WritePrivateProfileString(
  LPCTSTR lpAppName,  // セクション名
  LPCTSTR lpKeyName,  // キー名
  LPCTSTR lpString,   // 追加するべき文字列
  LPCTSTR lpFileName  // .ini ファイル
);
 
//==============================================================================
int main()
{
	String^ sectName = L"General";
	String^ fileName = L"C:\\tmp\\clrapi.ini";	// C:\WINDOWSに作られたりする
	StringBuilder^ retStr = gcnew StringBuilder(4);
	UINT ui;
	DWORD dw;
 
	WritePrivateProfileString(sectName, L"x", L"4649", fileName);
	ui = GetPrivateProfileInt(sectName, L"x", 0, fileName);
	Console::WriteLine(L"x=[{0}]", ui);
	dw = GetPrivateProfileString(sectName, L"x", L"", retStr, retStr->Capacity, fileName);
	Console::WriteLine(L"dw=[{0}] x=[{1}]", dw, retStr);
	Console::ReadLine();
	return 0;
}
 

出力
x=[4649]
dw=[3] x=[464]
最終更新:2012年09月28日 06:06