きったんの頭

HOME > C

Visual C++

| メモ | リンク |

メモ

現在日時

DateTime now = DateTime::Now;
Console::WriteLine(now.ToString(L"yyyy-MM-dd HH:mm:ss"));

メモリリークの検出

メモリリークの自動検出をしてみる
#if _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif

::_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);

DLL の遅延ロード

DLL の遅延ロード - ほっしーの技術ネタ備忘録
typedef void (__stdcall *FUNC_SampleAPI)();

HINSTANCE hInst;
FUNC_SampleAPI SampleAPI;

hInst = ::LoadLibrary(L"SampleDLL");
SampleAPI = (FUNC_SampleAPI)::GetProcAddress(hInst, "SampleAPI");

SampleAPI();

::FreeLibrary(hInst);

WAVE音声再生

const System::String^ path = System::IO::Path::GetDirectoryName(Application::StartupPath) + "\\";
System::Media::SoundPlayer^ soundPlayer = nullptr;

void StopSound(void) {
  if (soundPlayer != nullptr) {
    soundPlayer->Stop();
    soundPlayer = nullptr;
  }
}

void PlaySound(System::String^ fileName) {
  if (soundPlayer != nullptr) {
    StopSound();
  }
  soundPlayer = gcnew System::Media::SoundPlayer(path + fileName);
  soundPlayer->Play();
}

PlaySound("SAMPLE.WAV");

System::String^ から char* に変換

System::String^ sampleString = gcnew System::String("Sample String");
char* pString = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(sampleString).ToPointer();
// do something
System::Runtime::InteropServices::Marshal::FreeHGlobal(IntPtr(pString));