[前言:]编程中遇到的问题很多,再优秀的程序员也不会没有问题,但很多解决方法被淹没在论坛浩瀚的“水”中了,为了便于同道查询同时保存这些精华,我将在实际编程中常见的问题以及论坛中优秀的回贴收集起来以专题的形式发表。本文中的所有问题及回答均来自论坛。
问题一:如何实现指定盘符的光驱弹出弹入
钥匙在这里:
void ctrl_cdrom_door(
lpctstr drivename ,//驱动器的名字如f:等.
bool fopen file://弹出时用true,弹入时用false
)
{
tchar devstr[128],ctrlstr[128];
wsprintf(devstr,_t("open %s type cdaudio alias mycd wait"),drivename);
wsprintf(ctrlstr,_t("set mycd door %s wait"),fopen?_t("open"):_t("closed"));
mcisendstring(devstr,null,0,null);
mcisendstring(ctrlstr,null,0,null);
mcisendstring(_t("close mycd wait"),null,0,null);
}
file://测试的例子代码.
void cmainframe::ontestopen()
{
// todo: add your command handler code here
ctrl_cdrom_door("f:",true);
}
void cmainframe::ontestclose()
{
// todo: add your command handler code here
ctrl_cdrom_door("f:",false);
}
问题二:如何实现繁简体互换?
钥匙在这里:
// j2f.cpp : 简体(gb)==>繁体==>big5的过程
// 反向转换是类似的.
// 注意直接从简体-->big5不能做到一一对应.会有很多?出现,
// 故此需要先转成繁体.再转成big5.
// 我感觉这种方法应当和winnt或office里提供的繁简或字符集互转是一致的.
#include "stdafx.h"
#include
#include
#include
#include
#include
using namespace std;
void j2f(const string &s)
{
int n=s.length ();
int r=lcmapstring(
makelcid(makelangid(lang_chinese,sublang_chinese_simplified),sort_chinese_prc),
lcmap_traditional_chinese,
s.c_str (),s.length (),null,0);
if (!r) cout <<"error :"< char *ft=new char[r+1];
r=lcmapstring(
makelcid(makelangid(lang_chinese,sublang_chinese_simplified),sort_chinese_prc),
lcmap_traditional_chinese,
s.c_str (),s.length (),ft,r+1);//这个api搞掂简体转繁体,下面会打印繁体出来
if (r) {
ft[r]=0;
cout< wchar_t *pws=new wchar_t[r+1];
int r1=multibytetowidechar(936,0,ft,r,pws,r+1);
bool f=false;
r1=widechartomultibyte(950,0,pws,r1,ft,r+1,"?",&f);//代码页切换搞掂gb->big5
ft[r1]=0;
cout< for (int i=0;i cout<<"";
printf("0x%02x ",(byte)ft[i]);
}
cout<<")"< delete [] pws;
}
delete []ft;
}
//从标准输入简体国标-->big5繁体标准输出,输入两个空行退出
int main(int argc, char* argv[])
{
for(;;){
char line[1024];
cin.getline (line,sizeof(line));
string s(line);
if (!cin ||s.length ()==0) break;
j2f(s);
}
_getch();
return 0;
}
问题三:多线程中如何得到视图指针?
钥匙在这里:有两种方法可以实现你的要求:
1)第一种方法:
要是多线程不是在app.cpp里出现,那么要在多线程的.cpp中加上extern cyourapp theapp;
//获得文档模板:
position curtemplatepos = theapp.getfirstdoctemplateposition();
cdoctemplate *m_doc=theapp.getnextdoctemplate(curtemplatepos);
file://获得文档:
curtemplatepos=m_doc->getfirstdocposition();
cyourdoc *m_pdoc=(ca8doc*)m_doc->getnextdoc(curtemplatepos);
file://获得视图:
curtemplatepos=m_pdoc->getfirstviewposition();
cyourview *m_pview=(cyourview*)m_pdoc->getnextview(curtemplatepos);
file://调用视图函数:
m_pview->put();
2)第二种方法:
//获得窗体指针:
cmainframe *pframe = (cmainframe*)afxgetapp()->m_pmainwnd;
file://获得与该窗体符合的视图:
cyourview *m_pview = (cyourview *) pframe->getactiveview();
file://调用视图函数:
m_pview->put();
问题四:如何使程序在启动时不创建一个新文档?
钥匙在这里: 在程序的initinstance中的processshellcommand函数之前加入:
cmdinfo.m_nshellcommand = ccommandlineinfo::filenothing
问题五:如何在mdi程序中得到所有的视图?
钥匙在这里:
必须用一些文档中没有记载的函数:
cdocument::getfirstviewposition(); // doccore.cpp
cdocument::getnextview(); // doccore.cpp
cmultidoctemplate::getfirstdocposition(); // docmulti.cpp
cmultidoctemplate::getnextdoc(); // docmulti.cpp
同时还需要与cwinapp的成员m_templatelist打交道。
问题六: ado中如何得到某个数据库中的所有表的数目?
钥匙在这里:
hresult hr = s_ok;
_connectionptr pconnection = null;
_catalogptr pcatalog = null;
_bstr_t strcnn("provider=sqloledb;data source=myserver;"
"initial catalog=pubs;user id=sa;password=;");
try
{
file://define a command object for a stored procedure.
pconnection.createinstance(__uuidof(connection));
hr = pcatalog.createinstance(__uuidof (catalog));
hr = pconnection->open(strcnn,"","",adconnectunspecified);
pcatalog->putactiveconnection(_variant_t((idispatch *) pconnection));
long ntbcount = pcatalog->tables->count;//这就是你想要的表的数目
pconnection->close();
pconnection = null;
}
catch(_com_error &e)
{
....
}
问题七:从应用角度讲阻塞与非阻塞socket有什么区别?
钥匙在这里: