win32将当前目录切换到程序所在目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int pathGetContainer(const char *path, string &dir)
{
	const char *p;
	for (p = path + strlen(path); p >= path; p--)
	{
		if (*p == '/' || *p == '\\')
			break;
	}
	dir = string(path).substr(0, p - path + 1);
	return 1;
}
 
...
	char curPath[1024];
	GetModuleFileNameA(AfxGetApp()->m_hInstance, curPath, sizeof(curPath));
	string curDir;
	pathGetContainer(curPath, curDir);
	SetCurrentDirectoryA(curDir.c_str());
...

注:此方法对于Windows shell编程不适用,因为是运行在资源管理器进程里的插件,返回的都是explorer.exe所在路径,此种情况下,一般是通过注册表里写死的配置获取进程路径。

DeleteFile 5 unlink EACCES

查了很多网页,有的说是要删除的文件存在打开的句柄未关掉。而我这种情况是在程序一开始DeleteFile删除某个文件就删不掉,报ERROR_ACCESS_DENIED的错,换成_unlink删也删不掉。

但我在同样的目录新建一个文本文件再DeleteFile此文本文件是可以删除的。

于是我怀疑是不是有钩子进程使坏,我将“QQ电脑管家”程序退出,结果就可以成功调用DeleteFile了。

也就是说,“QQ电脑管家”阻止了DeleteFile删除某些特定后缀的文件,比如jpg后缀的文件。

printf Stack overflow

可能原因是栈空间用光了,需要仔细检查上下文中,看有没有存在占很大空间的局部变量。有的话,改为堆上分配这些变量。

读取jpg文件到HBITMAP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
HBITMAP loadPictureFromFile(LPCTSTR szFile)
{
	HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
	if (INVALID_HANDLE_VALUE == hFile)
		return NULL;
 
	DWORD dwFileSize = GetFileSize(hFile, NULL);
 
	LPVOID pvData = NULL;
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
 
	pvData = GlobalLock(hGlobal);
 
	DWORD dwBytesRead = 0;
	BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
	GlobalUnlock(hGlobal);
	CloseHandle(hFile);
 
	LPSTREAM pStm = NULL;
	HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
 
	LPPICTURE gpPicture;
 
	hr = ::OleLoadPicture(pStm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture);
 
	pStm->Release();
 
	OLE_HANDLE picHandle;
	gpPicture->get_Handle(&picHandle);
	HDC hdc = ::CreateCompatibleDC(NULL);
	::SelectObject(hdc, (HGDIOBJ)picHandle);
 
	HBITMAP hRet = (HBITMAP)::GetCurrentObject(hdc, OBJ_BITMAP);
	DeleteDC(hdc);
 
//	gpPicture->Release(); // 这里不能释放gpPicture,否则返回的hRet是野指针数据
 
	return hRet;
}

MFC CScrollBar拖动完后总跑到零位置

原因是

  1. 要处理SB_ENDSCROLL事件。
  2. 一定要在适当的地方GetScrollPos或SetScrollPos。

正确示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void SampleDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	int iMin = 0, iMax = 0;
	pScrollBar->GetScrollRange(&iMin, &iMax);
 
	// nPos = pScrollBar->GetScrollPos(); // 此处这句话一定不能有
 
	switch (nSBCode)
	{
	case SB_THUMBTRACK:
		break;
	case SB_LINELEFT:
		nPos = pScrollBar->GetScrollPos(); // 此处这句话一定要有
		nPos--;
		if (nPos < (UINT)iMin)
			nPos = 0;
		break;
	case SB_LINERIGHT:
		nPos = pScrollBar->GetScrollPos(); // 此处这句话一定要有
		nPos++;
		if (nPos >= (UINT)iMax)
			nPos = iMax - 1;
		break;
	case SB_ENDSCROLL:
		nPos = pScrollBar->GetScrollPos(); // 此处这句话一定要有
		break;
	default:
		break;
	}
 
	pScrollBar->SetScrollPos(nPos, 1); // 此处这句话也一定要有
 
	// Todo real work with nPos.
 
	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}

error C4839: 作为可变参数函数的参数的非标准用法

报错语句为:

1
	_stprintf_s(filepath, _T("%s\\%s"), curpath, CA2T(iniFilePath));

改为如下就不报错了:

1
2
	CString iniFilePathT = CA2T(iniFilePath);
	_stprintf_s(filepath, _T("%s\\%s"), curpath, (LPCTSTR)iniFilePathT);

targetSdkVersion未安装应用程式

发布后在Android 6.1的手机上安装不了,显示“未安装应用程序”,检查源码发现build.gradle文件中

1
2
3
4
    defaultConfig {
        minSdkVersion 14
	targetSdkVersion 27
    }

于是,把targetSdkVersion改为23,而发布release版时,Signature Versions只选V1(Jar Signature)。
再将生成的apk包在安卓6.1手机上装,顺利安装成功。

HLSL显示贴图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
float4x4 worldViewProj : WorldViewProjection;
 
struct VS_INPUT
{
	float3 pos : POSITION;
	float2 tex : TEXCOORD0;
};
 
struct VS_OUTPUT
{
	float4 pos : POSITION;
	float2 tex : TEXCOORD0;  
};
 
VS_OUTPUT mainVS(VS_INPUT input)
{
	VS_OUTPUT vsOut;	
	vsOut.pos = mul(float4(input.pos, 1), worldViewProj);
	vsOut.tex = input.tex;
//	vsOut.tex.xy = vsOut.pos.zw;
	return vsOut;
}
 
texture tex0;
 
sampler2D sample0 = 
sampler_state
{
	Texture = <tex0>;
/*	MipFilter = LINEAR;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
 
	AddressU = CLAMP;
	AddressV = CLAMP;
*/
};
 
float4 mainPS(float2 tex : TEXCOORD0) : COLOR
{
	return tex2D(sample0, tex);
//	return tex.x / tex.y;
}
 
technique technique0 {
	pass p0 {
		VertexShader = compile vs_3_0 mainVS();
		PixelShader = compile ps_3_0 mainPS();
	}
}

yv12 to nv21

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void nv212Yv12(char *nv21, char *yv12, int width, int height)
{
    int frameSize = width * height;
    memcpy(yv12, nv21, frameSize);
    nv21 += frameSize;
    yv12 += frameSize;
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    int quadFrame = halfWidth * halfHeight;
    for (int i = 0; i < halfHeight; i++) {
        for (int j = 0; j < halfWidth; j++) {
            *(yv12 + i * halfWidth + j) = *nv21++;
            *(yv12 + quadFrame + i * halfWidth + j) = *nv21++;
        }
    }
}
 
void yv122Nv21(char *yv12, char *nv21, int width, int height)
{
    int frameSize = width * height;
    memcpy(nv21, yv12, frameSize);
    nv21 += frameSize;
    yv12 += frameSize;
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    int quadFrame = halfWidth * halfHeight;
    for (int i = 0; i < halfHeight; i++) {
        for (int j = 0; j < halfWidth; j++) {
            *nv21++ = *(yv12 + i * halfWidth + j);
            *nv21++ = *(yv12 + quadFrame + i * halfWidth + j);
        }
    }
}