Hui's profile逆风沉沦BlogLists Tools Help
4/30/2007

test

2/25/2007

test

test
1/8/2007

test

怎么由dll文件生成对应的lib文件

我有一个DLL及对应的头文件,如何生成VC所要的LIB文件。 
 
1. dumpibn   /exports   Your.dll   >   Your.def  
  edit   Your.def,Let   it   become   standard   def   file  
  (  
  like:  
   
  EXPORTS:  
    abc  
    def  
   
  )  
   
  lib   /def:Your.def   /out:Your.lib  
 

2. 有了头文件,说明你已经知道导出函数。  
  可以这样:使用楼上兄弟提到的工具查到对应函数导出函数写一个DEF文件:  
  LIBRARY   youdll.dll  
  EXPORTS  
          Func1@0                             @2  
   
  然后  
  lib   /MACHINE:IX86   /DEF:yourLIB.DEF  
1/5/2007

test

test

高效拆分字符串

 

inline int stringSplit1(const char* lpszToSplit,char delemit, const char* lpszStartArray[],int nSizeArray[],int nArraySize)
{
        if(!lpszToSplit)
                return 0;
        int             nPart   = 0;   
        int             nSize   = 0;
        bool    bNew    = true;
        for(;*lpszToSplit;lpszToSplit++)
        {      
                if(bNew)
                {              
                        nPart++;
                        if(nPart > nArraySize)
                                break;
                       
                        bNew = false;
                        lpszStartArray[nPart-1] = lpszToSplit;
                        nSize = 0;
                }
               
                if(*lpszToSplit==delemit)
                {      
                        bNew =true;            
                        nSizeArray[nPart-1] = nSize;
                }
                else
                {
                        nSize++;
                }
        }
        if(!bNew)
                nSizeArray[nPart-1] = nSize;
        return nPart > nArraySize ? -1 : nPart ;
}
12/19/2006

test

写了一个分割字符串的函数

void stringSplit(const std::string& strToSplit,const std::string& strDelimit, std::vector<std::string>& vecStringOutput)
{
        vecStringOutput.clear();
        std::string::size_type begin,end;
        begin = strToSplit.find_first_not_of(strDelimit);
        while(begin!=std::string::npos)
        {
                end = strToSplit.find_first_of(strDelimit,begin);
                vecStringOutput.push_back(strToSplit.substr(begin,end-begin));
                begin = strToSplit.find_first_not_of(strDelimit,end);
        }
}
 
void stringSplit(const std::string& strToSplit,char delimit, std::vector<std::string>& vecStringOutput)
{
        vecStringOutput.clear();
        std::string::size_type begin=0,end=0,length=strToSplit.length();
        while(begin<length && end!=std::string::npos)
        {
                end = strToSplit.find(delimit,begin);
                vecStringOutput.push_back(strToSplit.substr(begin,end-begin));
                begin = end + 1;
        }
}
11/24/2006

test

test

ifstream, ofstream 有时打开带中文路径的文件会失败

似乎跟文件系统是ntfs还是fat有关, 解决的办法如下:
 
1. 使用c函数
setlocale(LC_ALL,"Chinese-simplified");
 
2. 使用stl
std::locale::global(std::locale(""));
 
11/15/2006

test

test
10/20/2006

TEST

TEST
9/26/2006

test

CHyperLink动态创建时存在问题

CHyperLink动态创建时存在问题
 CHyperLink是网上流传很广的一个超链接类, 但是我在使用过程中发现, 动态创建该类时会出现问题, 经跟踪调试发现问题出在CHyperLink类的PreSubclassWindow中. 先看看代码:

void CHyperLink::PreSubclassWindow()
{     
 // If the URL string is empty try to set it to the window text
    if (m_strURL.IsEmpty())
        GetWindowText(m_strURL);

    // Check that the window text isn't empty.
 // If it is, set it as URL string.
    CString strWndText;
    GetWindowText(strWndText);
    if (strWndText.IsEmpty()) {
  // Set the URL string as the window text
        ASSERT(!m_strURL.IsEmpty());    // window text and URL both NULL!
  CStatic::SetWindowText(m_strURL);
    }

    // Get the current window font 
    CFont* pFont = GetFont(); 
 
 if (pFont != NULL) {
  LOGFONT lf;
  pFont->GetLogFont(&lf);
  lf.lfUnderline = BITSET(m_dwStyle, StyleUnderline);
  if (m_Font.CreateFontIndirect(&lf))
   CStatic::SetFont(&m_Font);   
  // Adjust window size to fit URL if necessary
  AdjustWindow();
 }
 else {  
  // if GetFont() returns NULL then probably the static
  // control is not of a text type: it's better to set
  // auto-resizing off
  CLEARBITS(m_dwStyle,StyleAutoSize);
 }
 
 if (!BITSET(m_dwStyle,StyleNoHandCursor))
  SetDefaultCursor();      // Try to load an "hand" cursor

    // Create the tooltip
    CRect rect;
    GetClientRect(rect);
    m_ToolTip.Create(this); 

    m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);

    CStatic::PreSubclassWindow();
}

通常我们动态创建该类都是这样写的:
CHyperLink *pHyperLink = new CHyperLink;
pHyperLink->Create("hello",WS_CHILD|WS_VISIBLE,CRect(0,0,100,100),this);

很少有人会在调用Create之前先调用SetURL, 这样 ASSERT(!m_strURL.IsEmpty())这句会断言失败, 为了避免这种情况, 必须记得在Create之前先调用SetURL, 给m_strURL赋上值, 或者将这里改为:
if(!m_strURL.IsEmpty())
    CStatic::SetWindowText(m_strURL);

另一个严重的问题是m_ToolTip.Create(this)会引起崩溃, 解决的方法是将以下几句
    CRect rect;
    GetClientRect(rect);
    m_ToolTip.Create(this); 
    m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
移动到OnCreate中去.

CHyperLink类见如下链接:
http://www.codeguru.com/Cpp/controls/controls/hyperlinkcontrols/article.php/c2185#more

9/1/2006

test

8/15/2006

test

test
7/21/2006

test

7/19/2006

test

6/13/2006

test

test

c++:在类的构造函数中调用另一个构造函数

c++:在类的构造函数中调用另一个构造函数

    在java里,经常可见类的构造函数调用另一个构造函数,但是在c++里,由于构造函数允许有默认参数,使得这种需求大为减少。虽然这样,也许偶尔我们还是希望在类的构造函数里调用另一个构造函数。我们知道,构造一个对象时会做两件事:1,分配内存 2,执行构造函数;所以在构造函数里调用另一个构造函数的关键是让第二个构造函数在第一次分配好的内存上执行,而不是分配新的内存,这个可以用标准库的placement new做到:

    先看看标准库中placement new的定义
    inline void *__cdecl operator new(size_t, void *_P)
        {return (_P); }

    可见没有分配新的内存。
 
   
#include <new>

class my
{
public:
        my()
        {
                new (this) my(5);
        }

        my(int i)
        {
                a=i;
        }

        int a;
};

    使用这个方法需要注意,如果第一个构造函数里初始化了某个成员变量,然后调用另一个构造函数,在这个构造函数里又初始化了同一个成员变量,这样就会造成同一个成员变量初始化了两次。但这种问题在java中也存在,编程时注意一下顺序就好了。

6/8/2006

test

test

CDialog窗口类的Class Style中没有CS_VREDRAW和CS_HREDRAW导致自绘有问题

CDialog窗口类的Class Style中默认没有CS_VREDRAW和CS_HREDRAW导致自绘有问题
 
    最近为了美化程序,需要在CDialog的派生类中自绘一些东西,其中最简单的自绘是在对话框客户区边沿画一些线条(在OnPaint中绘图),但是这种最简单的自绘也会产生问题,我的对话框是能改变大小的,当改变对话框大小的时候,总会残留改变大小之前的绘图,用Spy++查看对话框的Class Style,发现对话框在默认状态下的Class Style是CS_SAVEBITS和CS_DBLCLKS,并没有CS_VREDRAW和CS_HREDRAW,这样当对话框大小改变的时候,只有新增加的区域得到了重绘,而原来的区域保留了上次自绘时的状态,所以产生了残留,在OnInitDialog中调用
 
SetClassLong(m_hWnd,GCL_STYLE,CS_VREDRAW|CS_HREDRAW|CS_DBLCLKS);
 
人为的加上CS_VREDRAW|CS_HREDRAW属性,影像残留得到了解决。

    看来MFC对CDialog的绘图做了一些优化,可能是因为CDialog的背景比较简单,直接在Dialog上绘图的也比较少见吧。但是这种优化对在Dialog里自绘造成了一些麻烦。
  
 

让对话框背景透明

在OnCtlColor里返回一个空的HBRUSH即可:
    hbr = (HBRUSH)::GetStockObject(NULL_BRUSH);