VC++ メモリの動的確保とクラス

  build.bat



  
cmd.exe /c vc6.bat mem
  

  
Set AddPath=C:\Program Files\Microsoft Visual Studio\COMMON\MSDev98\Bin
Set PATH=%AddPath%;%PATH%
Set CL6="C:\Program Files\Microsoft Visual Studio\VC98\Bin\cl.exe"
Set LINK6="C:\Program Files\Microsoft Visual Studio\VC98\Bin\link.exe"
Set INC6="C:\Program Files\Microsoft Visual Studio\VC98\Include"
Set INCA="C:\Program Files\Microsoft Visual Studio\VC98\ATL\Include"
Set LIB6="C:\Program Files\Microsoft Visual Studio\VC98\Lib"
Set LIBS1=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib
Set LIBS2=shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib
%CL6% %1.cpp /c /I%INC6% /I%INCA%
%LINK6% /LIBPATH:%LIB6% %1.obj %LIBS1% %LIBS2%
  
↑vb6.bat



  mem.cpp



  
#include <windows.h>
#include <stdio.h>

void type_01();
void type_02();
void type_03();

// *********************************************************
// バッファクラス
// *********************************************************
class buffer {
public:
	// ***************************************
	// コンストラクタ1
	// ***************************************
	buffer(){
		buffer::nSize = 1024;
		buffer::lpBuffer = NULL;
		buffer::hMem = GetProcessHeap();
		buffer::lpBuffer = 
			(LPTSTR)HeapAlloc(buffer::hMem, 0, 1024); 
		if ( buffer::lpBuffer == NULL ) {
			printf( "%s\n", "メモリの確保に失敗しました" );
		}
	}
	// ***************************************
	// コンストラクタ2
	// ***************************************
	buffer( int nSize ){
		buffer::nSize = nSize;
		buffer::lpBuffer = NULL;
		buffer::hMem = GetProcessHeap();
		buffer::lpBuffer =
			(LPTSTR)HeapAlloc(buffer::hMem, 0, buffer::nSize); 
		if ( buffer::lpBuffer == NULL ) {
			printf( "%s\n", "メモリの確保に失敗しました" );
		}

	}
	virtual ~buffer(){
		HeapFree( this->hMem, 0, this->lpBuffer );
	}

	// ***************************************
	// キャストオペレータ
	// ***************************************
	operator char *( )
	{
		return( this->lpBuffer );
	}

	// ***************************************
	// 代入オペレータ
	// ***************************************
	void operator = ( LPTSTR str )
	{
		lstrcpy( this->lpBuffer, str );
	}
	void operator = ( buffer &str )
	{
		lstrcpy( this->lpBuffer, str.lpBuffer );
	}

	// ***************************************
	// 文字列追加オペレータ
	// ***************************************
	void operator += ( LPTSTR str )
	{
		lstrcat( this->lpBuffer, str );
	}
	void operator += ( buffer &str )
	{
		lstrcat( this->lpBuffer, str.lpBuffer );
	}

	LPTSTR lpBuffer;
	HANDLE hMem;
	int nSize;
};

int main() {

	type_01();
	type_02();
	type_03();

	return 0;
}

// *********************************************************
// スタンダード
// *********************************************************
void type_01()
{

	LPTSTR lpBuffer = NULL;
	HANDLE hMem = GetProcessHeap();
	int nSize = 1024;

	lpBuffer = (LPTSTR)HeapAlloc(hMem, 0, nSize); 
	if ( lpBuffer == NULL ) {
		printf( "%s\n", "メモリの確保に失敗しました" );
		return;
	}

	// Windows ディレクトリの取得
	GetWindowsDirectory( lpBuffer, nSize );
	printf( "%s\n", lpBuffer );

	HeapFree( hMem, 0, lpBuffer );

}

// *********************************************************
// バッファクラスを使用
// *********************************************************
void type_02()
{

	buffer data(1024);

	// Windows ディレクトリの取得
	GetWindowsDirectory( data.lpBuffer, data.nSize );
	printf( "%s\n", data.lpBuffer );

}


// *********************************************************
// オペレータを使用
// *********************************************************
void type_03()
{

	buffer data(1024),file;

	file = "myfile.txt";

	// Windows ディレクトリの取得
	GetWindowsDirectory( (LPTSTR)data, data.nSize );

	data += "\\system32\\";
	data += file;

	printf( "%s\n", (LPTSTR)data );

}
  










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





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

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ