2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#→C++用のDLL作成で詰まったところのメモ

Last updated at Posted at 2015-05-29

std::stringを使用するには

*#include

TAR32.DLLを使用するには

●下準備
 1.下記URLからパッケージをダウンロード
 http://www.csdinc.co.jp/archiver/lib/tar32.html

 2.ダウンロードしたパッケージからTAR32.DLLからコピーして自分のcppと同列に置く

下記ソースコードを参照

●宣言部

typedef int (WINAPI *LPUNTAREXTRACTMEM)(const HWND, LPCSTR, LPBYTE, const DWORD, int *, LPWORD, LPDWORD);
LPUNTAREXTRACTMEM lpTarExtractMem = NULL;
HMODULE hTar32 = 0;

●実装部

std::string DllName = "TAR32.DLL";

size_t origsize = DllName.size() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t wcstring[newsize];
mbstowcs_s(&convertedChars, wcstring, origsize, DllName.c_str(), _TRUNCATE);

lpTarExtractMem = (LPUNTAREXTRACTMEM)GetProcAddress(hTar32, "TarExtractMem");

bool testret = true;
hTar32 = LoadLibrary(wcstring);

指定URLからダウンロードするためには

下記インクルード

  • #include "wininet.h"

下記ソースファイルを参照

●URLを渡すとダウンロードしてlpszSrcにデータがセットされる。

void HttpDownLoadAction(std::string url){

	HINTERNET hInet, hUrl;
	char szBuf[128], *lpszSrc;

	DWORD dwRead, dwTotal = 0;	// ファイルサイズ
	HGLOBAL hMem;
	char szUrl[128];
	strcpy_s(szUrl, url.c_str());
	//インターネットの開始
	hInet = InternetOpen((LPCWSTR)"test-program",
		INTERNET_OPEN_TYPE_PRECONFIG,
		NULL, NULL, 0);
	if (hInet == NULL){
		return;
	}

	size_t origsize = url.size() + 1;
	const size_t newsize = 100;
	size_t convertedChars = 0;
	wchar_t wcstring[newsize];
	mbstowcs_s(&convertedChars, wcstring, origsize, szUrl, _TRUNCATE);


	//HTTPセッションの開始、指定のURLオープン
	hUrl = InternetOpenUrl(hInet, wcstring, NULL, 0, 0, 0);
	if (hUrl == NULL){
		return;
	}
	hMem = GlobalAlloc(GHND, 1);
	lpszSrc = (char*)GlobalLock(hMem);
	while (1){
		InternetReadFile(
			hUrl, szBuf, (DWORD)sizeof(szBuf) - 1, &dwRead);
		szBuf[dwRead] = '\0';
		if (dwRead == 0)break;
		dwTotal += dwRead;
		hMem = GlobalReAlloc(hMem, dwTotal + 1, GMEM_MOVEABLE);
		if (hMem == NULL){
			return;
		}
		lpszSrc = (char*)GlobalLock(hMem);
		if (lpszSrc == NULL){
			break;
		}
		strcat_s(lpszSrc, dwTotal + 1, szBuf);
	}
	//m_edit_display.SetWindowTextA(lpszSrc);
	//メモリの解放
	GlobalUnlock(hMem);
	GlobalFree(hMem);
	//インターネットハンドルの解放
	InternetCloseHandle(hUrl);
	InternetCloseHandle(hInet);
}

wininet.h を自分のDLLで使用するには

●Visual Studioのメニュー→プロジェクト→プロジェクト名のプロパティ

●図のように追加の依存ファイルにWininet.libを追加

スクリーンショット 2015-05-29 13.29.38.png

●ソリューションをビルドしなおすとリンクエラーが消える

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?