SQL Server essence

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  37 随笔 :: 8 文章 :: 2 评论 :: 0 引用

公告

2009年7月8日 #

osql.exe -S %TARGETSERVER% -E -d Northwind -Q"select db_id()" > nul
if %errorlevel% == 0 (
    @echo.
    @echo Northwind DB already exists.
    @echo.
    goto end
)
@echo.
@echo Creating Northwind DB ...
@echo.
osql.exe -S !TARGETSERVER! -E -n -i !WORKINGDIR!\instnwnd90.sql


:end

posted @ 2009-07-08 18:43 天蝎 阅读(11) 评论(0) 编辑

'CString' to 'std::string':

std::string cannot always construct from a LPCTSTR i.e. the code will fail for UNICODE builds.

So the following conversion will lead to error:

CString cs("Hello");
std::string s((LPCTSTR)cs);


As std::string can construct only from LPSTR / LPCSTR, a programmer who uses VC++ 7.x or better can utilize conversion classes such as CT2CA as an intermediary.

  CString cs ("Hello");
  // Convert a TCHAR string to a LPCSTR
  CT2CA pszConvertedAnsiString (cs);
  // construct a std::string using the LPCSTR input
  std::string strStd (pszConvertedAnsiString);

 

'std::string' to 'CString':

std::string s("Hello");
CString cs(s.c_str());

posted @ 2009-07-08 14:38 天蝎 阅读(237) 评论(0) 编辑

Original link: http://www.codeguru.com/forum/showthread.php?t=436716

A: An easy way is to use FindFirstChangeNotification, FindNextChangeNotification and one of the wait functions in a worker thread. As for example:

Code:
DWORD WINAPI FolderFilesWatchDogThread(LPVOID lpParam) // thread procedure
{
   HANDLE hFileChange = 
      ::FindFirstChangeNotification((LPCTSTR)lpParam, // folder path
                                    FALSE,            // don't look in subfolders
                                    FILE_NOTIFY_CHANGE_FILE_NAME); 
                                                      // watch for
                                                      // renaming, creating, 
                                                      // or deleting a file
   if(INVALID_HANDLE_VALUE == hFileChange)
   {
      DWORD dwError = ::GetLastError();
      // handle error (see this FAQ)
      return dwError;
   }
   
   while(TRUE)
   {
      ::WaitForSingleObject(hFileChange, INFINITE);
      // Bark, bark!!! A file was renamed, created or deleted.
      ::FindNextChangeNotification(hFileChange);
   }
   return 0;
}

   // somewhere in the space...
   ::CreateThread(NULL, 0, FolderFilesWatchDogThread, _T("c:\\temp"), 0, NULL);
A little bit more sophisticated but offering more info (like the name of the file) is to use ReadDirectoryChangesW.
Also, here is a simplified example:

Code:
DWORD WINAPI FolderWatchThread(LPVOID lpParam) // thread procedure
{
   HANDLE hDirectory =
      ::CreateFile((LPCTSTR)lpParam,    // folder path 
                   FILE_LIST_DIRECTORY,
                   FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
                   NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
   
   if(INVALID_HANDLE_VALUE == hDirectory)
   {
      DWORD dwError = ::GetLastError();
      // handle error (see this FAQ)
      return dwError;
   }

   DWORD dwBytesReturned = 0;
   const DWORD dwBuffLength = 4096;
   BYTE buffer[dwBuffLength];
   WCHAR wchFileName[_MAX_PATH + 1];
   
   while(::ReadDirectoryChangesW(hDirectory, buffer, dwBuffLength, FALSE,
                                 FILE_NOTIFY_CHANGE_FILE_NAME, &dwBytesReturned, 
                                 NULL, NULL))
   {
      DWORD dwNextEntryOffset = 0;
      PFILE_NOTIFY_INFORMATION pfni = NULL;
      do
      {
         pfni = (PFILE_NOTIFY_INFORMATION)(buffer + dwNextEntryOffset);
         
         switch(pfni->Action)
         {
         case FILE_ACTION_ADDED: 
            // The file was added to the directory. 
            break;
         case FILE_ACTION_REMOVED: 
            // The file was removed from the directory. 
            break;
         case FILE_ACTION_RENAMED_OLD_NAME: 
            // The file was renamed and this is the old name. 
            break;
         case FILE_ACTION_RENAMED_NEW_NAME: 
            // The file was renamed and this is the new name.
            break;
            // ...
         }
         memcpy(wchFileName, pfni->FileName, pfni->FileNameLength);
         wchFileName[pfni->FileNameLength / sizeof(WCHAR)] = L'\0';
         // Enjoy of added, removed, or modified file name...
         dwNextEntryOffset += pfni->NextEntryOffset; // next please!
      }while(pfni->NextEntryOffset != 0);
   } 
   ::CloseHandle(hDirectory);
   return 0;
}
__________________
posted @ 2009-07-08 13:33 天蝎 阅读(61) 评论(0) 编辑

1.

#ifndef HEADER_BASE_HPP
#define HEADER_BASE_HPP

class base_class
{
  // some code
};

#endif // HEADER_BASE_HPP

 

The first line checks whether we have not yet defined 'HEADER_BASE_HPP'. If we have not, then we define it and include the rest of the file. If it is already defined, i.e. if this header file has already been included, then we will just skip until the end of the file and nothing more will get dumped into the '.cpp' file.

 

2.

#pragma once
// content of the header
class base_class
{
};

 

On Microsoft compilers guarding can be done using #pragma once, as the first pre-processor directive in a header.

posted @ 2009-07-08 12:20 天蝎 阅读(34) 评论(0) 编辑


  在Windows的shellapi文件中定义了一个名为SHFileOperation()的外壳函数,用它可以实现各种文件操作,如文件的拷贝、删除、移动等,该函数使用起来非常简单,它只有一个指向SHFILEOPSTRUCT结构的参数。使用SHFileOperation()函数时只要填写该专用结构--SHFILEOPSTRUCT,告诉Windows执行什么样的操作,以及其它重要信息就行了。SHFileOperation()的特别之处在于它是一个高级外壳函数,不同于低级文件处理。当调用SHFileOperation操作文件时,相应的外壳拷贝处理器(如果有的话)被调用。如在删除某个文件时,SHFileOperation会将删除的文件放到Recycle Bin中。SHFileOperation()函数的原形为:

WINSHELLAPI int WINAPI SHFileOperation (LPSHFILEOPSTRUCT lpFIleOp);

  函数中参数类型为一个LPSHFILEOPSTRUCT结构,它包含有进行文件操作的各种信息,其具体的结构如下:

Typedef struct _ShFILEOPSTRUCT
{
HWND hWnd; //消息发送的窗口句柄;
UINT wFunc; //操作类型
LPCSTR pFrom; //源文件及路径
LPCSTR pTo; //目标文件及路径
FILEOP_FLAGS fFlags; //操作与确认标志
BOOL fAnyOperationsAborted; //操作选择位
LPVOID hNameMappings; //文件映射
LPCSTR lpszProgressTitle; //文件操作进度窗口标题
}SHFILEOPSTRUCT, FAR * LPSHFILEOPSTRUCT;

  在这个结构中,hWnd是指向发送消息的窗口句柄,pFrom与pTo是进行文件操作的源文件名和目标文件名,它包含文件的路径,对应单个文件的路径字符串,或对于多个文件,必须以NULL作为字符串的结尾或文件路径名之间的间隔,否则在程序运行的时候会发生错误。另外,pFrom和pTo都支持通配符*和?,这大大方便了开发人员的使用。例如,源文件或目录有两个,则应是:char pFrom[]="d:\\Test1\0d:\\Text.txt\0",它表示对要D:盘Test目录下的所有文件和D:盘上的Text.txt文件进行操作。字符串中的"\\"是C语言中的'\'的转义符,'\0'则是NULL。wFunc 是结构中的一个非常重要的成员,它代表着函数将要进行的操作类型,它的取值为如下:

  FO_COPY: 拷贝文件pFrom到pTo 的指定位置。

  FO_RENAME: 将pFrom的文件名更名为pTo的文件名。

  FO_MOVE: 将pFrom的文件移动到pTo的地方。

  FO_DELETE: 删除pFrom指定的文件。

  使用该函数进行文件拷贝、移动或删除时,如果需要的时间很长,则程序会自动在进行的过程中出现一个无模式的对话框(Windows操作系统提供的文件操作对话框),用来显示执行的进度和执行的时间,以及正在拷贝、移动或删除的文件名,此时结构中的成员lpszProgressTitle显示此对话框的标题。fFlags是在进行文件操作时的过程和状态控制标识。它主要有如下一些标识,也可以是其组合:

  FOF_FILESONLY:执行通配符,只执行文件;

  FOF_ALLOWUNDO:保存UNDO信息,以便在回收站中恢复文件;

  FOF_NOCONFIRMATION:在出现目标文件已存在的时候,如果不设置此项,则它会出现确认是否覆盖的对话框,设置此项则自动确认,进行覆盖,不出现对话框。

  FOF_NOERRORUI:设置此项后,当文件处理过程中出现错误时,不出现错误提示,否则会进行错误提示。

  FOF_RENAMEONCOLLISION:当已存在文件名时,对其进行更换文提示。

  FOF_SILENT:不显示进度对话框。

  FOF_WANTMAPPINGHANDLE:要求SHFileOperation()函数返回正处于操作状态的实际文件列表,文件列表名柄保存在hNameMappings成员中。

  SHFILEOPSTRUCT结构还包含一个SHNAMEMAPPING结构的数组,此数组保存由SHELL计算的每个处于操作状态的文件的新旧路径。

  在使用该函数删除文件时必须设置SHFILEOPSTRUCT结构中的神秘FOF_ALLOWUNDO标志,这样才能将待删除的文件拷到Recycle Bin,从而使用户可以撤销删除操作。需要注意的是,如果pFrom设置为某个文件名,用FO_DELETE标志删除这个文件并不会将它移到Recycle Bin,甚至设置FOF_ALLOWUNDO标志也不行,在这里你必须使用全路径名,这样SHFileOperation才会将删除的文件移到Recycle Bin。
posted @ 2009-07-08 12:03 天蝎 阅读(443) 评论(0) 编辑