Win7调试服务程序,Debugbreak函数不响应,直接退出

上周调试Win7下的一个服务程序,以前都是用Debugbreak()直接可以进入调试的,但这次直接退出了。

查了一下,这样设置一下就可以:
控制面板->操作中心->维护->检查问题报告的解决方案->设置
每次发生问题时,在检查解决方案之前先询问我

保存设置后,就可以进入断点了,再次鄙视微软。

后来,又发现,即使设置后,仍然无法响应断点,没办法,只好用比较挫的代码搞定了:

	//在第一个DebugBreak()前面,添加下面的语句
	while(!IsDebuggerPresent())
	{
		Sleep(100);
	}
	DebugBreak();

这样,服务启动后,会一直等待调试器。启动服务后,手动通过VS、任务管理器或Process Explorer附加到进程,就可以对启动的服务进行调试了。

参考:
DebugBreak not breaking

Android Custom Protocol中intent-filter的data标签

当在Android中使用Custom Protocol时,需要在AndroidManifest.xml中,增加相应的intent-filter标签。
其中intent-filter下的data标签,通知了Android,何时处理调用请求,其语法如下:

    <data android:scheme="string"
          android:host="string"
          android:port="string"
          android:path="string"
          android:pathPattern="string"
          android:pathPrefix="string"
          android:mimeType="string" />

其实,上述内容描述了一个URI,当第三方调阅请求不符合该URI时,会直接忽略;而当第三方调阅请求符合该URI时,则会进行调用或让用户进行选择。

URI的具体规则如下:

<scheme>://<host>:<port>/[<path>|<pathPrefix>|<pathPattern>]

如果没有指定scheme,则整个URI无效;
如果没有指定host,则port属性无效;

其各属性说明如下:
scheme:就是协议名称,如http,https,myprotocol等,建议全部小写,而且不包含“:”。
host:ip地址或域名,建议全部小写。
port:端口
path:完全路径匹配
pathPrefix:路径前缀匹配
pathPattern:路径匹配规则(例如:“*”表示字符重复出现0到多次;“.”表示任意字符;“.*”表示0到多个任意字符;转义符为“\”,由于xml的格式限制,非转义的“*”要写为“\\*”,非转移的“\”要写为“\\\\”。)
mimeType:指定MIME类型,如image/jpeg,其中子类型可以使用通配符“*”,建议全部小写。

	<!--http协议下的任何pdf文件-->
	<intent-filter>
		<action android:name="android.intent.action.VIEW"/>
		<category android:name="android.intent.category.DEFAULT"/>
		<data android:scheme="http" android:pathPattern=".*\\.pdf"]</data>
	</intent-filter>

	<!--任何音频文件-->
	<intent-filter>
		<action android:name="android.intent.action.VIEW" />
		<category android:name="android.intent.category.DEFAULT" />
		<data android:mimeType="audio/*" />
	</intent-filter>

	<!--自定义私有协议myproto的任何请求-->
	<intent-filter>
		<action android:name="android.intent.action.VIEW" />
		<category android:name="android.intent.category.DEFAULT" />
		<data android:scheme="myproto"/>
	</intent-filter>

参考资料:
Android开发手册
newcj的博客

IOS实现Custom Protocol

在IOS中,实现Custom Protocol,需要以下两步:

1、修改Info.plist文件,增加以下内容:

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>com.neohope.custom.protocol.test</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>myproto</string>
            </array>
        </dict>
    </array>

2、在AppDelegate.m中,增加以下内容:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if(url!=nil)
    {
        NSString *path = url.absoluteString;
        //do your job here
    }
    return YES;
}

Andoid实现Custom Protocol

Android中实现Custom Protocol,只需要简单的两步:

1、修改AndroidManifest.xml文件,在activity节增加以下内容:

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="myproto"/>
            </intent-filter>

2、修改对应activity中,用以下代码,就可以处理URL中的数据啦

        Intent intent = getIntent();
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri uri = intent.getData();
            String user = uri.getQueryParameter("user");
            String password = uri.getQueryParameter("password");
            //do your job here
        }

汉字繁简体互换

1、chs2cht.h

#pragma once
char* UnicodeToBIG5(const wchar_t* szUnicodeString);
char* UnicodeToGB2312(const wchar_t* szUnicodeString);
wchar_t* GB2312ToUnicode(const char* szGBString);
char* GB2312ToBIG5(const char* szGBString);

2、chs2cht.cpp

#include "chs2cht.h"
#include <atlstr.h>
//GB2312 转 Unicode:
wchar_t* GB2312ToUnicode(const char* szGBString)  
{  
	UINT nCodePage = 936; //GB2312  
	int nLength=MultiByteToWideChar(nCodePage,0,szGBString,-1,NULL,0);  
	wchar_t* pBuffer = new wchar_t[nLength+1];  
	MultiByteToWideChar(nCodePage,0,szGBString,-1,pBuffer,nLength);  
	pBuffer[nLength]=0;  
	return pBuffer;  
}

//BIG5 转 Unicode:  
wchar_t* BIG5ToUnicode(const char* szBIG5String)  
{  
	UINT nCodePage = 950; //BIG5  
	int nLength=MultiByteToWideChar(nCodePage,0,szBIG5String,-1,NULL,0);  
	wchar_t* pBuffer = new wchar_t[nLength+1];  
	MultiByteToWideChar(nCodePage,0,szBIG5String,-1,pBuffer,nLength);  
	pBuffer[nLength]=0;  
	return pBuffer;  
}

//Unicode 转 GB2312:  
char* UnicodeToGB2312(const wchar_t* szUnicodeString)  
{  
	UINT nCodePage = 936; //GB2312  
	int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);  
	char* pBuffer=new char[nLength+1];  
	WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);  
	pBuffer[nLength]=0;  
	return pBuffer;  
}

//Unicode 转 BIG5:  
char* UnicodeToBIG5(const wchar_t* szUnicodeString)  
{  
	UINT nCodePage = 950; //BIG5  
	int nLength=WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,NULL,0,NULL,NULL);  
	char* pBuffer=new char[nLength+1];  
	WideCharToMultiByte(nCodePage,0,szUnicodeString,-1,pBuffer,nLength,NULL,NULL);  
	pBuffer[nLength]=0;  
	return pBuffer;  
}

//繁体中文BIG5 转 简体中文GB2312  
char* BIG5ToGB2312(const char* szBIG5String)  
{  
	LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);  
	wchar_t* szUnicodeBuff = BIG5ToUnicode(szBIG5String);  
	char* szGB2312Buff = UnicodeToGB2312(szUnicodeBuff);  
	int nLength = LCMapStringA(lcid,LCMAP_SIMPLIFIED_CHINESE, (LPCSTR)szGB2312Buff,-1,NULL,0);  
	char* pBuffer = new char[nLength + 1];  
	LCMapStringA(0x0804,LCMAP_SIMPLIFIED_CHINESE,(LPCSTR)szGB2312Buff,-1,pBuffer,nLength);  
	pBuffer[nLength] = 0;  

	delete[] szUnicodeBuff;  
	delete[] szGB2312Buff;  
	return pBuffer;  
}

//简体中文GB2312 转 繁体中文BIG5  
char* GB2312ToBIG5(const char* szGBString)  
{  
	LCID lcid = MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC);  
	int nLength = LCMapStringA(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,NULL,0);  
	char* pBuffer=new char[nLength+1];  
	LCMapStringA(lcid,LCMAP_TRADITIONAL_CHINESE,szGBString,-1,pBuffer,nLength);  
	pBuffer[nLength]=0;  
	wchar_t* pUnicodeBuff = GB2312ToUnicode(pBuffer);  
	char* pBIG5Buff = UnicodeToBIG5(pUnicodeBuff);  
	delete[] pBuffer;  
	delete[] pUnicodeBuff;  
	return pBIG5Buff;  
}

3、test.cpp

#include "chs2cht.h"
#include <iostream> 
#include <locale.h>
#include <atlstr.h>  

using namespace std;

int main(int argc, char** argv)
{
    //“区域设置”为简体中文 
    locale loc( "chs" ); 
    char str[100];  
    cin>>str;  
    
    char * rlt=GB2312ToBIG5(str);  
    CString cStr1;   
    cStr1.Format( TEXT("%s"),rlt); 

    //“区域设置”为繁体中文 
    setlocale(LC_ALL, ".950");  
    cout<<rlt<<endl; 

    return 0;
}

VC fix .pch file missing

表现:

Error   1   fatal error C1083: Cannot open precompiled header file: 'Debug\????.pch': No such file or directory

解决方法:
1、保证stdafx.h及stdafx.cpp在项目的最顶层,stdafx.h用于保存需要的头文件,stdafx.cpp中只有对stdafx.h的引用;
2、在VS中,右击需要修改的工程,选择”Properties”;
3、在左上角,选择“ All Configurations”;
4、在左边,找到“C/C++”,并定位到“Precompiled Headers”;
5、将选项Precompiled Header修改为: “Use (/Yu)”;
6、将选项“Precompiled Header File”修改为:“stdafx.h”;
7、保存设置;
8、保证#include “stdafx.h”为所有需要预编译的cpp文件的第一行;
9、VS中,右击stdafx.cpp,选择”Properties”;
10、在左上角,选择“ All Configurations”;
11、将选项Precompiled Header修改为: “Create (/Yc).”;
12、保存设置,重新编译。

Prolog101(08)

列表:
放在用方括号中的一组项目的集合,各项目之间使用逗号分割。

空表:
没有项目的列表,用[]表示。

表头与表尾:
[X|Y]可以与任意的列表匹配,匹配成功后,X绑定为列表的第一个项目的值,我们称之为表头(head)。
而Y则绑定为剩下的列表,我们称之为表尾(tail)。
表尾(tail)一定是列表,而表头(head)则是一个项目,该项目可以是表,也可以是其他的任何数据结构。
看下这个例子就清楚了:

1 ?- [a|[b,c,d]] = [a,b,c,d]. 
true.

2 ?- [a|b,c,d] = [a,b,c,d].
ERROR: Syntax error: Unexpected comma or bar in rest of list
...

3 ?- [H|T] = [a]. 
H = a,
T = [].
 
4 ?- [H|T] = [a,b,c,d]. 
H = a,
T = [b, c, d].

5 ?- [H|T] = [a,[b,c,d]]. 
H = a,
T = [[b, c, d]].

6 ?- [H|T] = [].
false.

7 ?- [A,B|T] = [a,b,c,d].
A = a,
B = b,
T = [c, d].

8 ?- [a|[b|[c|[d|[]]]]] = [a,b,c,d].
true.

检查数据是否存在:

1 ?- member(a, [a,b,c]). 
true .

2 ?- member(d, [a,b,c]). 
false.

3 ?- member(d, [a,b,c,[d],e]). 
false.

4 ?- member([d], [a,b,c,[d],e]).
true .

5 ?- member(X, [a,b,c]). 
X = a ;
X = b ;
X = c.

追加

1 ?- append([a,b,c],[d,e,f],X).
X = [a, b, c, d, e, f].

2 ?- append([],[d,e,f],X).
X = [d, e, f].

3 ?- append([a,b],Y,[a,b,c,d]).
Y = [c, d].

4?- append(X,Y,[a,b,c]).
X = [],
Y = [a, b, c] ;
X = [a],
Y = [b, c] ;
X = [a, b],
Y = [c] ;
X = [a, b, c],
Y = [] ;

删除

1 ?- delete([a,b,c,d,e], c, X).
X = [a, b, d, e]

Prolog101(07)

联合(Unification)

变量与任何项目: 变量可以与任何项目绑定,其中也包括变量
原始项目与原始项目: 两个原始项目(原子或整数)只有当它们相同时才能联合。
结构与结构: 如果两个结构的每个相应的参数能联合,那么这两个结构可以联合。

1 ?- (1,2,3)=(X,Y,Z).
X = 1,
Y = 2,
Z = 3.

2 ?- (1,2,3)=(X,X,Z).
false.

3 ?- (1,1,3)=(X,X,Z).
X = 1,
Z = 3.

4 ?- (1,X,3)=(X,1,Z).
X = 1,
Z = 3.

5 ?- (1,X,3)=(X,2,Z).
false.
1 ?- (1,2,X)=(1,2,(3,4,5)).
X = (3, 4, 5).

2 ?- (1,2,X)=(1,2,(3,4,Y)),Y=5.
X = (3, 4, 5),
Y = 5.

%_表示不关心匹配内容
3 ?- (1,2,X,Y,7)=(1,2,(3,4,5),6,_).
X = (3, 4, 5),
Y = 6.

4 ?- X = Y, Y = hi, write(X).
hi
X = Y, Y = hi.
%swipl -s objs.pl
%Hansen

%room
room(kitchen).

%objects and location
%object(Name, Color, Size, Weight).
location(object(candle, red, small, 1), kitchen).
location(object(apple, red, small, 1), kitchen).
location(object(apple, green, small, 1), kitchen).
location(object(table, blue, big, 50), kitchen).

%current room
here(kitchen).

%can take something?
can_take(Thing) :-  
here(Room), 
location(object(Thing, _, small, _), Room). 
can_take(Thing) :- 
here(Room), 
location(object(Thing, _, big, _), Room), 
write('The '), write(Thing),  
write(' is too big to carry.'), nl, 
fail. 
can_take(Thing) :- 
here(Room), 
not(location(object(Thing, _, _, _), Room)), 
write('There is no '), write(Thing), write(' here.'), nl, 
fail.

%out put the weight
write_weight(1) :- write('1 pound').  
write_weight(W) :- W > 1, write(W), write(' pounds').

%list all things in a room
list_things(Place) :-  
location(object(Thing, Color, Size, Weight), Place), 
write('A '),write(Size),tab(1), 
write(Color),tab(1), 
write(Thing), write(', weighing '), 
write_weight(Weight), nl, 
fail.