积累系统性知识
积聚技术精华
  首页    个人中心    撰写积文    建立课题    订立目标    整理积文    管理课题    管理目标    技能Get    代码积累 
编程技巧搜集(2)
error997 (error997)    2014-11-20 20:26:20      目标    课题
   1.ComboBox中如何进行 动态查询(BCB)
切换到: 纯代码  
   
//---------------------------------------------------------------------------
   #include <vcl.h>
#pragma hdrstop
   #include <assert.h>       //++++++++
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
   //---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//===========================================================================
//功能:检测Value 是否在Lp..Hp之间
bool __fastcall In(int Lp, int Hp,int Value)
{
        assert(Lp<=Hp);
        return ((Value<=Hp)&&(Value>=Lp));
}
//参数:一个汉字
//返回值:该汉字的拼音
char __fastcall GetPYChar(AnsiString HZ)
{
assert(HZ.Length()==2);
WORD Hi=WORD(HZ[1])<<8;
WORD Lo=BYTE(HZ[2]);
int n=Hi+Lo;
if (In(0xB0A1,0xB0C4,n)) return 'A';
if (In(0XB0C5,0XB2C0,n)) return 'B';
if (In(0xB2C1,0xB4ED,n)) return 'C';
if (In(0xB4EE,0xB6E9,n)) return 'D';
if (In(0xB6EA,0xB7A1,n)) return 'E';
if (In(0xB7A2,0xB8c0,n)) return 'F';
if (In(0xB8C1,0xB9FD,n)) return 'G';
if (In(0xB9FE,0xBBF6,n)) return 'H';
if (In(0xBBF7,0xBFA5,n)) return 'J';
if (In(0xBFA6,0xC0AB,n)) return 'K';
if (In(0xC0AC,0xC2E7,n)) return 'L';
if (In(0xC2E8,0xC4C2,n)) return 'M';
if (In(0xC4C3,0xC5B5,n)) return 'N';
if (In(0xC5B6,0xC5BD,n)) return 'O';
if (In(0xC5BE,0xC6D9,n)) return 'P';
if (In(0xC6DA,0xC8BA,n)) return 'Q';
if (In(0xC8BB,0xC8F5,n)) return 'R';
if (In(0xC8F6,0xCBF0,n)) return 'S';
if (In(0xCBFA,0xCDD9,n)) return 'T';
if (In(0xCDDA,0xCEF3,n)) return 'W';
if (In(0xCEF4,0xD188,n)) return 'X';
if (In(0xD1B9,0xD4D0,n)) return 'Y';
if (In(0xD4D1,0xD7F9,n)) return 'Z';
return char(0);
}
//===========================================================================
void __fastcall TForm1::ComboBox1KeyPress(TObject *Sender, char &Key)
{
  char c;
  for(int i=0;i<ComboBox1->Items->Count;i++){
    c= GetPYChar(ComboBox1->Items->Strings[i].SubString(0,2));
    if(toupper(c)==toupper(Key)){
      Key=NULL;
      ComboBox1->ItemIndex=i;break;
      }
    }
   }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  ComboBox1->Items->Clear();
  ComboBox1->Text="";
  ComboBox1->Items->Add("长沙");
  ComboBox1->Items->Add("北京");
  ComboBox1->Items->Add("武汉");
  ComboBox1->Items->Add("桂林");
}

   2.实现重起删除文件
ForceKiller软件中,这样描述到"此方式不是直接删除文件,因此文件不会立即被删除而是通知系统核心SMSS在下次开机时由核心自动删除!"
实现:
切换到: 纯代码  
   
MoveFileEx(szDstFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);

它是写入注册表,等到启动时.smss从注册表读出然后删除,之后smss才引导win32子系统.
   3.如何把一个程序加入windows防火墙的白名单
切换到: 纯代码  
   
   // WindowsFirewall.h: interface for the CWindowsFirewall class.
//
//////////////////////////////////////////////////////////////////////
   #if !defined(AFX_WINDOWSFIREWALL_H__9F175C70_A4B7_4ECF_9A01_5B72FBE9C5C0__INCLUDED_)
#define AFX_WINDOWSFIREWALL_H__9F175C70_A4B7_4ECF_9A01_5B72FBE9C5C0__INCLUDED_
   #if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <netfw.h>
   //windows防火墙查询控制类。 
class CWindowsFirewall  
{
public:
CWindowsFirewall();
virtual ~CWindowsFirewall();
HRESULT Initialize(OUT INetFwProfile** fwProfile);
void Cleanup(IN INetFwProfile* fwProfile);
HRESULT IsOn(IN INetFwProfile* fwProfile, OUT BOOL* fwOn);
HRESULT TurnOn(IN INetFwProfile* fwProfile);
HRESULT TurnOff(IN INetFwProfile* fwProfile);
HRESULT AppIsEnabled(
IN INetFwProfile* fwProfile,
IN const wchar_t* fwProcessImageFileName,
OUT BOOL* fwAppEnabled

);
切换到: 纯代码  
   
HRESULT AddApp(
IN INetFwProfile* fwProfile,
IN const wchar_t* fwProcessImageFileName,
IN const wchar_t* fwName

);
切换到: 纯代码  
   
HRESULT PortIsEnabled(
IN INetFwProfile* fwProfile,
IN LONG portNumber,
IN NET_FW_IP_PROTOCOL ipProtocol,
OUT BOOL* fwPortEnabled

);
切换到: 纯代码  
   
   HRESULT PortAdd(
IN INetFwProfile* fwProfile,
IN LONG portNumber,
IN NET_FW_IP_PROTOCOL ipProtocol,
IN const wchar_t* name

);
切换到: 纯代码  
   
   int Test();
   };
#endif // !defined(AFX_WINDOWSFIREWALL_H__9F175C70_A4B7_4ECF_9A01_5B72FBE9C5C0__INCLUDED_)
   
////////////////////////////////////
// CWindowsFirewall::.cpp: implementation of the CCWindowsFirewall:: class.
//
//////////////////////////////////////////////////////////////////////
   #include "stdafx.h"
#include <objbase.h>
#include "WindowsFirewall.h"
//#include <windows.h>
#include <crtdbg.h>
   
#include <objbase.h>
#include <oleauto.h>
#include <stdio.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
   //////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
   CWindowsFirewall::CWindowsFirewall()
{
   }
   CWindowsFirewall::~CWindowsFirewall()
{
   }
/*
    Copyright (c) Microsoft Corporation
   SYNOPSIS
   Sample code for the Windows Firewall COM interface.
*/
   
   HRESULT CWindowsFirewall::Initialize(OUT INetFwProfile** fwProfile)
{
    HRESULT hr = S_OK;
    INetFwMgr* fwMgr = NULL;
    INetFwPolicy* fwPolicy = NULL;
   _ASSERT(fwProfile != NULL);
   *fwProfile = NULL;
   // Create an instance of the firewall settings manager.
    hr = CoCreateInstance(
            __uuidof(NetFwMgr),
            NULL,
            CLSCTX_INPROC_SERVER,
            __uuidof(INetFwMgr),
            (void**)&fwMgr

            );
切换到: 纯代码  
   
    if (FAILED(hr))
    {
        printf("CoCreateInstance failed: 0x%08lx/n", hr);
        goto error;
    }
   // Retrieve the local firewall policy.
    hr = fwMgr->get_LocalPolicy(&fwPolicy);
    if (FAILED(hr))
    {
        printf("get_LocalPolicy failed: 0x%08lx/n", hr);
        goto error;
    }
   // Retrieve the firewall profile currently in effect.
    hr = fwPolicy->get_CurrentProfile(fwProfile);
    if (FAILED(hr))
    {
        printf("get_CurrentProfile failed: 0x%08lx/n", hr);
        goto error;
    }

   error:
切换到: 纯代码  
   
   // Release the local firewall policy.
    if (fwPolicy != NULL)
    {
        fwPolicy->Release();
    }
   // Release the firewall settings manager.
    if (fwMgr != NULL)
    {
        fwMgr->Release();
    }
   return hr;
}
   
void CWindowsFirewall::Cleanup(IN INetFwProfile* fwProfile)
{
    // Release the firewall profile.
    if (fwProfile != NULL)
    {
        fwProfile->Release();
    }
}
   
HRESULT CWindowsFirewall::IsOn(IN INetFwProfile* fwProfile, OUT BOOL* fwOn)
{
    HRESULT hr = S_OK;
    VARIANT_BOOL fwEnabled;
   _ASSERT(fwProfile != NULL);
    _ASSERT(fwOn != NULL);
   *fwOn = FALSE;
   // Get the current state of the firewall.
    hr = fwProfile->get_FirewallEnabled(&fwEnabled);
    if (FAILED(hr))
    {
        printf("get_FirewallEnabled failed: 0x%08lx/n", hr);
        goto error;
    }
   // Check to see if the firewall is on.
    if (fwEnabled != VARIANT_FALSE)
    {
        *fwOn = TRUE;
        printf("The firewall is on./n");
    }
    else
    {
        printf("The firewall is off./n");
    }

   error:
切换到: 纯代码  
   
   return hr;
}
   
HRESULT CWindowsFirewall::TurnOn(IN INetFwProfile* fwProfile)
{
    HRESULT hr = S_OK;
    BOOL fwOn;
   _ASSERT(fwProfile != NULL);
   // Check to see if the firewall is off.
    hr = CWindowsFirewall::IsOn(fwProfile, &fwOn);
    if (FAILED(hr))
    {
        printf("CWindowsFirewall::IsOn failed: 0x%08lx/n", hr);
        goto error;
    }
   // If it is, turn it on.
    if (!fwOn)
    {
        // Turn the firewall on.
        hr = fwProfile->put_FirewallEnabled(VARIANT_TRUE);
        if (FAILED(hr))
        {
            printf("put_FirewallEnabled failed: 0x%08lx/n", hr);
            goto error;
        }
   printf("The firewall is now on./n");
    }

   error:
切换到: 纯代码  
   
   return hr;
}
   HRESULT CWindowsFirewall::TurnOff(IN INetFwProfile* fwProfile)
{
    HRESULT hr = S_OK;
    BOOL fwOn;
   _ASSERT(fwProfile != NULL);
   // Check to see if the firewall is on.
    hr = CWindowsFirewall::IsOn(fwProfile, &fwOn);
    if (FAILED(hr))
    {
        printf("CWindowsFirewall::IsOn failed: 0x%08lx/n", hr);
        goto error;
    }
   // If it is, turn it off.
    if (fwOn)
    {
        // Turn the firewall off.
        hr = fwProfile->put_FirewallEnabled(VARIANT_FALSE);
        if (FAILED(hr))
        {
            printf("put_FirewallEnabled failed: 0x%08lx/n", hr);
            goto error;
        }
   printf("The firewall is now off./n");
    }

   error:
切换到: 纯代码  
   
   return hr;
}
   
HRESULT CWindowsFirewall::AppIsEnabled(
            IN INetFwProfile* fwProfile,
            IN const wchar_t* fwProcessImageFileName,
            OUT BOOL* fwAppEnabled
            )
{
    HRESULT hr = S_OK;
    BSTR fwBstrProcessImageFileName = NULL;
    VARIANT_BOOL fwEnabled;
    INetFwAuthorizedApplication* fwApp = NULL;
    INetFwAuthorizedApplications* fwApps = NULL;
   _ASSERT(fwProfile != NULL);
    _ASSERT(fwProcessImageFileName != NULL);
    _ASSERT(fwAppEnabled != NULL);
   *fwAppEnabled = FALSE;
   // Retrieve the authorized application collection.
    hr = fwProfile->get_AuthorizedApplications(&fwApps);
    if (FAILED(hr))
    {
        printf("get_AuthorizedApplications failed: 0x%08lx/n", hr);
        goto error;
    }
   // Allocate a BSTR for the process image file name.
    fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName);
    if (fwBstrProcessImageFileName == NULL)
    {
        hr = E_OUTOFMEMORY;
        printf("SysAllocString failed: 0x%08lx/n", hr);
        goto error;
    }
   // Attempt to retrieve the authorized application.
    hr = fwApps->Item(fwBstrProcessImageFileName, &fwApp);
    if (SUCCEEDED(hr))
    {
        // Find out if the authorized application is enabled.
        hr = fwApp->get_Enabled(&fwEnabled);
        if (FAILED(hr))
        {
            printf("get_Enabled failed: 0x%08lx/n", hr);
            goto error;
        }
   if (fwEnabled != VARIANT_FALSE)
        {
            // The authorized application is enabled.
            *fwAppEnabled = TRUE;
   printf(

                "Authorized application %lS is enabled in the firewall./n",
                fwProcessImageFileName
                );
切换到: 纯代码  
   
        }
        else
        {
            printf(

                "Authorized application %lS is disabled in the firewall./n",
                fwProcessImageFileName
                );
切换到: 纯代码  
   
        }
    }
    else
    {
        // The authorized application was not in the collection.
        hr = S_OK;
   printf(

            "Authorized application %lS is disabled in the firewall./n",
            fwProcessImageFileName
            );
切换到: 纯代码  
   
    }

   error:
切换到: 纯代码  
   
   // Free the BSTR.
    SysFreeString(fwBstrProcessImageFileName);
   // Release the authorized application instance.
    if (fwApp != NULL)
    {
        fwApp->Release();
    }
   // Release the authorized application collection.
    if (fwApps != NULL)
    {
        fwApps->Release();
    }
   return hr;
}
   
HRESULT CWindowsFirewall::AddApp(
            IN INetFwProfile* fwProfile,
            IN const wchar_t* fwProcessImageFileName,
            IN const wchar_t* fwName
            )
{
    HRESULT hr = S_OK;
    BOOL fwAppEnabled;
    BSTR fwBstrName = NULL;
    BSTR fwBstrProcessImageFileName = NULL;
    INetFwAuthorizedApplication* fwApp = NULL;
    INetFwAuthorizedApplications* fwApps = NULL;
   _ASSERT(fwProfile != NULL);
    _ASSERT(fwProcessImageFileName != NULL);
    _ASSERT(fwName != NULL);
   // First check to see if the application is already authorized.
    hr = CWindowsFirewall::AppIsEnabled(
            fwProfile,
            fwProcessImageFileName,
            &fwAppEnabled

            );
切换到: 纯代码  
   
    if (FAILED(hr))
    {
        printf("CWindowsFirewall::AppIsEnabled failed: 0x%08lx/n", hr);
        goto error;
    }
   // Only add the application if it isn't already authorized.
    if (!fwAppEnabled)
    {
        // Retrieve the authorized application collection.
        hr = fwProfile->get_AuthorizedApplications(&fwApps);
        if (FAILED(hr))
        {
            printf("get_AuthorizedApplications failed: 0x%08lx/n", hr);
            goto error;
        }
   // Create an instance of an authorized application.
        hr = CoCreateInstance(
                __uuidof(NetFwAuthorizedApplication),
                NULL,
                CLSCTX_INPROC_SERVER,
                __uuidof(INetFwAuthorizedApplication),
                (void**)&fwApp

                );
切换到: 纯代码  
   
        if (FAILED(hr))
        {
            printf("CoCreateInstance failed: 0x%08lx/n", hr);
            goto error;
        }
   // Allocate a BSTR for the process image file name.
        fwBstrProcessImageFileName = SysAllocString(fwProcessImageFileName);
        if (fwBstrProcessImageFileName == NULL)
        {
            hr = E_OUTOFMEMORY;
            printf("SysAllocString failed: 0x%08lx/n", hr);
            goto error;
        }
   // Set the process image file name.
        hr = fwApp->put_ProcessImageFileName(fwBstrProcessImageFileName);
        if (FAILED(hr))
        {
            printf("put_ProcessImageFileName failed: 0x%08lx/n", hr);
            goto error;
        }
   // Allocate a BSTR for the application friendly name.
        fwBstrName = SysAllocString(fwName);
        if (SysStringLen(fwBstrName) == 0)
        {
            hr = E_OUTOFMEMORY;
            printf("SysAllocString failed: 0x%08lx/n", hr);
            goto error;
        }
   // Set the application friendly name.
        hr = fwApp->put_Name(fwBstrName);
        if (FAILED(hr))
        {
            printf("put_Name failed: 0x%08lx/n", hr);
            goto error;
        }
   // Add the application to the collection.
        hr = fwApps->Add(fwApp);
        if (FAILED(hr))
        {
            printf("Add failed: 0x%08lx/n", hr);
            goto error;
        }
   printf(

            "Authorized application %lS is now enabled in the firewall./n",
            fwProcessImageFileName
            );
切换到: 纯代码  
   
    }

   error:
切换到: 纯代码  
   
   // Free the BSTRs.
    SysFreeString(fwBstrName);
    SysFreeString(fwBstrProcessImageFileName);
   // Release the authorized application instance.
    if (fwApp != NULL)
    {
        fwApp->Release();
    }
   // Release the authorized application collection.
    if (fwApps != NULL)
    {
        fwApps->Release();
    }
   return hr;
}
   HRESULT CWindowsFirewall::PortIsEnabled(
            IN INetFwProfile* fwProfile,
            IN LONG portNumber,
            IN NET_FW_IP_PROTOCOL ipProtocol,
            OUT BOOL* fwPortEnabled
            )
{
    HRESULT hr = S_OK;
    VARIANT_BOOL fwEnabled;
    INetFwOpenPort* fwOpenPort = NULL;
    INetFwOpenPorts* fwOpenPorts = NULL;
   _ASSERT(fwProfile != NULL);
    _ASSERT(fwPortEnabled != NULL);
   *fwPortEnabled = FALSE;
   // Retrieve the globally open ports collection.
    hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts);
    if (FAILED(hr))
    {
        printf("get_GloballyOpenPorts failed: 0x%08lx/n", hr);
        goto error;
    }
   // Attempt to retrieve the globally open port.
    hr = fwOpenPorts->Item(portNumber, ipProtocol, &fwOpenPort);
    if (SUCCEEDED(hr))
    {
        // Find out if the globally open port is enabled.
        hr = fwOpenPort->get_Enabled(&fwEnabled);
        if (FAILED(hr))
        {
            printf("get_Enabled failed: 0x%08lx/n", hr);
            goto error;
        }
   if (fwEnabled != VARIANT_FALSE)
        {
            // The globally open port is enabled.
            *fwPortEnabled = TRUE;
   printf("Port %ld is open in the firewall./n", portNumber);
        }
        else
        {
            printf("Port %ld is not open in the firewall./n", portNumber);
        }
    }
    else
    {
        // The globally open port was not in the collection.
        hr = S_OK;
   printf("Port %ld is not open in the firewall./n", portNumber);
    }

   error:
切换到: 纯代码  
   
   // Release the globally open port.
    if (fwOpenPort != NULL)
    {
        fwOpenPort->Release();
    }
   // Release the globally open ports collection.
    if (fwOpenPorts != NULL)
    {
        fwOpenPorts->Release();
    }
   return hr;
}
   
HRESULT CWindowsFirewall::PortAdd(
            IN INetFwProfile* fwProfile,
            IN LONG portNumber,
            IN NET_FW_IP_PROTOCOL ipProtocol,
            IN const wchar_t* name
            )
{
    HRESULT hr = S_OK;
    BOOL fwPortEnabled;
    BSTR fwBstrName = NULL;
    INetFwOpenPort* fwOpenPort = NULL;
    INetFwOpenPorts* fwOpenPorts = NULL;
   _ASSERT(fwProfile != NULL);
    _ASSERT(name != NULL);
   // First check to see if the port is already added.
    hr = CWindowsFirewall::PortIsEnabled(
            fwProfile,
            portNumber,
            ipProtocol,
            &fwPortEnabled

            );
切换到: 纯代码  
   
    if (FAILED(hr))
    {
        printf("CWindowsFirewall::PortIsEnabled failed: 0x%08lx/n", hr);
        goto error;
    }
   // Only add the port if it isn't already added.
    if (!fwPortEnabled)
    {
        // Retrieve the collection of globally open ports.
        hr = fwProfile->get_GloballyOpenPorts(&fwOpenPorts);
        if (FAILED(hr))
        {
            printf("get_GloballyOpenPorts failed: 0x%08lx/n", hr);
            goto error;
        }
   // Create an instance of an open port.
        hr = CoCreateInstance(
                __uuidof(NetFwOpenPort),
                NULL,
                CLSCTX_INPROC_SERVER,
                __uuidof(INetFwOpenPort),
                (void**)&fwOpenPort

                );
切换到: 纯代码  
   
        if (FAILED(hr))
        {
            printf("CoCreateInstance failed: 0x%08lx/n", hr);
            goto error;
        }
   // Set the port number.
        hr = fwOpenPort->put_Port(portNumber);
        if (FAILED(hr))
        {
            printf("put_Port failed: 0x%08lx/n", hr);
            goto error;
        }
   // Set the IP protocol.
        hr = fwOpenPort->put_Protocol(ipProtocol);
        if (FAILED(hr))
        {
            printf("put_Protocol failed: 0x%08lx/n", hr);
            goto error;
        }
   // Allocate a BSTR for the friendly name of the port.
        fwBstrName = SysAllocString(name);
        if (SysStringLen(fwBstrName) == 0)
        {
            hr = E_OUTOFMEMORY;
            printf("SysAllocString failed: 0x%08lx/n", hr);
            goto error;
        }
   // Set the friendly name of the port.
        hr = fwOpenPort->put_Name(fwBstrName);
        if (FAILED(hr))
        {
            printf("put_Name failed: 0x%08lx/n", hr);
            goto error;
        }
   // Opens the port and adds it to the collection.
        hr = fwOpenPorts->Add(fwOpenPort);
        if (FAILED(hr))
        {
            printf("Add failed: 0x%08lx/n", hr);
            goto error;
        }
   printf("Port %ld is now open in the firewall./n", portNumber);
    }

   error:
切换到: 纯代码  
   
   // Free the BSTR.
    SysFreeString(fwBstrName);
   // Release the open port instance.
    if (fwOpenPort != NULL)
    {
        fwOpenPort->Release();
    }
   // Release the globally open ports collection.
    if (fwOpenPorts != NULL)
    {
        fwOpenPorts->Release();
    }
   return hr;
}
   
int CWindowsFirewall::Test()
{
    HRESULT hr = S_OK;
    HRESULT comInit = E_FAIL;
    INetFwProfile* fwProfile = NULL;
   // Initialize COM.
    comInit = CoInitializeEx(
                0,
                COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE

                );
切换到: 纯代码  
   
   // Ignore RPC_E_CHANGED_MODE; this just means that COM has already been
   // initialized with a different mode. Since we don't care what the mode is,
   // we'll just use the existing mode.
   if (comInit != RPC_E_CHANGED_MODE)
   {
        hr = comInit;
        if (FAILED(hr))
        {
            TRACE("CoInitializeEx failed: 0x%08lx/n", hr);
            goto error;
        }
   }
   // Retrieve the firewall profile currently in effect.
    hr = CWindowsFirewall::Initialize(&fwProfile);
    if (FAILED(hr))
    {
        TRACE("CWindowsFirewall::Initialize failed: 0x%08lx/n", hr);
        goto error;
    }
   // Turn off the firewall.
//     hr = CWindowsFirewall::TurnOff(fwProfile);
//     if (FAILED(hr))
//     {
//         TRACE("CWindowsFirewall::TurnOff failed: 0x%08lx/n", hr);
//         goto error;
//     }
// 
//     // Turn on the firewall.
//     hr = CWindowsFirewall::TurnOn(fwProfile);
//     if (FAILED(hr))
//     {
//         TRACE("CWindowsFirewall::TurnOn failed: 0x%08lx/n", hr);
//         goto error;
//     }
   // Add Windows Messenger to the authorized application collection.
     hr = CWindowsFirewall::AddApp(
             fwProfile,
             L"%ProgramFiles%//Messenger//msmsgs.exe",
             L"Windows Messenger"

             );
切换到: 纯代码  
   
     if (FAILED(hr))
     {
         TRACE("CWindowsFirewall::AddApp failed: 0x%08lx/n", hr);
         goto error;
     }
   

   error:
切换到: 纯代码  
   
   // Release the firewall profile.
    CWindowsFirewall::Cleanup(fwProfile);
   // Uninitialize COM.
    if (SUCCEEDED(comInit))
    {
        CoUninitialize();
    }
   return 0;
}

   4.C语言中怎样判断汉字
其实判断汉字的代码很简单,简单到只有一行就可以了,但不理解汉字编码的基础,这一行代码却万万无法理解。
   常用的字符编码有以下三种:
1、ASCII 只支持英文,全部为8位
2、DBCS  支持英文和中文,但中文需要两个字节(16位)
3、UNICODE 支持英文和中文,英文和中文都需要两个字节
   ASCII是DOS时代的,无法支持中文。DBCS是Win9x支持的字符集。UNICODE是win2k和xp支持的字符集。
而汉字的编码目前有GB2312-1980和GB18030-2000,GB2312就是DBCS类型的汉字编码,GB18030就是UNICODE的汉字编码,当然GB18030兼容GB2312,也就是说GB18030也支持DBCS的字符处理方式。虽然GB18030是2000年后强制执行的国家标准,但目前使用最多的还是GB2312编码,而且GB2312也足够处理你所用到的汉字了。还有GBK编码是GB2312的增强版。
   这里我仅支持GB2312。由于GB2312是中国大陆制定的标准,所以繁体中文并不在GB2312的编码中,如果你的程序需要支持繁体中文,则还需要处理Big5编码。其实也很简单。
   下面说一下在C中如何处理GB2312编码的汉字。以VC6.0为例,如果声明变量的类型为wchar_t则是UNICODE编码,如果是char则是DBCS编码比如我的一个函数声明是:
切换到: 纯代码  
   
void Justify (HDC hdc, PTSTR pText, RECT * prc, int iAlign)    

其中的pText是PTSTR类型,PTSTR在WINNT.H中有两个定义(WINNT.H中的这段代码我删掉了中间无关的部分)
切换到: 纯代码  
   
#ifdef  UNICODE
typedef LPWSTR PTSTR, LPTSTR;//如果是UNICODE编码,则定义PTSTR为LPWSTR类型
#else
typedef LPSTR PTSTR, LPTSTR;//如果是不是UNICODE编码,则定义PTSTR为LPSTR类型
#endif

   LPSTR定义为CHAR的指针
LPSWSTR定义为WCHAR的指针
CHAR定义为char类型
WCHAR定义为wchar_t类型
而wchar_t定义为unsigned short类型,它是16位,两个字节,无符号短整数
   是UNICODE还是非UNICODE取决于你的编译选项,如果在[工程]-[选项]-[C/C++]的[预处理程序定义]中填入了_UNICODE,那么程序会用wchar_t指针来定义LPSTR,如果没有_UNICODE,那么程序会用char指针来定义LPSTR,这样带来的区别就是,你接受到的pText中的字节内容是不一样的,[i服了you]这个字串如果在没有定义_UNICODE的情况下,是8个字节,而在定义了_UNICODE的情况下是12个字节。反映到程序中就是,如果没有定义_UNICODE,那么就要把英文字符当成1个字节来处理,而汉字字符的编码是采用GB2312编码规范来的;如果定义了_UNICODE,那么英文字符要当成2个字节来处理,而汉字字符的编码是采用UNICODE编码来的。举例来说,win98不采用UNICODE编码而采用的是DBCS编码,为了让我的程序既可以在XP下运行又可以在Win98下运行,我没有定义_UNICODE。这样我的程序代码就要把字符串当成DBCS编码来处理,也就是英文字符是1个字节,中文字符是2个字节,中文编码采用GB2312编码。用Justify来说明:
我给pText传递来[你]这个汉字,那么pText应该有两个字节来存放[你]这个字,设置段点来读一下pText的内容。
切换到: 纯代码  
   
void Justify (HDC hdc, PTSTR pText, RECT * prc, int iAlign)
{
static TCHAR szText[] = {TEXT ("你")} ;
   pText=szText;
}

设断调试会发现*pText=-60,怎么会这样呢,原因是没有按unsigned char来转换*pText的值,修改代码如下:
切换到: 纯代码  
   
void Justify (HDC hdc, PTSTR pText, RECT * prc, int iAlign)
{
static TCHAR szText[] = {TEXT ("你")} ;
unsigned char sqChar[20];//这个变量就是为了强制转换类型用的
   pText=szText;
sqChar[0]=*pText;
sqChar[1]=*(pText+1);
}

这时下断查看sqChar[0]=196,sqChar[1]=227就对了,为什么呢,因为它和GB2312的编码是一样的。
GB2312-80编码的编码范围是高位0xa1-0xfe,低位是 0xa1-0xfe ,其中汉字范围为 0xb0a1 和 0xf7fe,如果只是简单地判断汉字,则只要查看高字节是否大于等于0xa1就可以了,还有就是,全角字符的高字节统统等于0xa3,所以很容易可以区别出全角字符来。
   如果你希望你的程序能支持到GB18030,那么就去找GB18030的规范来看看。
   网上有很多判断汉字的说法,你只要记住,你要支持的编码是哪个?GB2312、GBK、GB18030?每个编码有自己的编码范围或者规范,网上之所以有不同的说法,正是因为他们互相说的不是同一种编码方式。我这里说的是GB2312的编码,如果你的程序要编译成支持UNICODE的话,那么这段代码就要修改成对应UNICODE规范的代码了。
   附简单的测试汉字、全角字符、英文的程序:
切换到: 纯代码  
   
   //test.c
//源代码作者:夏克 sequh@126.com
//新建Win32 Application工程,把test.c加入,运行,试着修改szText的值,来观察代码效果
   #include <windows.h>
   int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szText[] = {TEXT ("i服,了。uy")} ;
PTSTR pText;
int i;
unsigned char sqChar[20];
   pText=szText;
while (*pText != '/0')
{
i=IsGB(pText);
   switch(i)
{
case 0:
pText++;
MessageBox (NULL, TEXT ("发现数字、英文字符或英文标点"), TEXT ("Hello"), 0);
break;
case 1:
pText++;
pText++;
MessageBox (NULL, TEXT ("发现全角字符"), TEXT ("Hello"), 0);
break;
case 2:
pText++;
pText++;
MessageBox (NULL, TEXT ("发现汉字"), TEXT ("Hello"), 0);
break;
}
}
   return 0 ;
}
   int IsGB(PTSTR pText)
{
unsigned char sqChar[20];
sqChar[0]=*pText;
if (sqChar[0]>=0xa1)
if (sqChar[0]==0xa3)
return 1;//全角字符
else
return 2;//汉字
else
return 0;//英文、数字、英文标点
}

   5.怎么把界面弹到最上层
在ShowWindow(SW_SHOW)后加入
切换到: 纯代码  
   
   SetForegroundWindow();

   6.文件关联怎样做
修改注册表   
  例子:   
    
  HKEY_CLASSES_ROOT   键下   
  [HKEY_CLASSES_ROOT/.mp3]   
  @="Mp3Player"   
    
  [HKEY_CLASSES_ROOT/Mp3Player]   
  @="Mp3Player"   
    
切换到: 纯代码  
   
  //图标   

  [HKEY_CLASSES_ROOT/AudioDecoder/defaultIcon]   
  @="C://Mp3Player.EXE,1"   
    
    
  [HKEY_CLASSES_ROOT/AudioDecoder/Shell]   
    
  [HKEY_CLASSES_ROOT/AudioDecoder/Shell/open]   
    
切换到: 纯代码  
   
  //   打开的程序   

  [HKEY_CLASSES_ROOT/AudioDecoder/Shell/open/command]   
  @="Mp3Player.EXE   %1"
   在CMyApp::InitInstance()中加入或修改以下代码   
切换到: 纯代码  
   
  EnableShellOpen();   
  RegisterShellFileTypes(TRUE);   
  CCommandLineInfo   cmdInfo;   
  ParseCommandLine(cmdInfo);   
  if(!ProcessShellCommand(cmdInfo))   
  return   FALSE;   
    

  在以下代码以前   
切换到: 纯代码  
   
  pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);   
  pMainFrame->UpdateWindow();

   7.如何通过HTTP下载一个文件
切换到: 纯代码  
   
#include <Urlmon.h>
#pragma comment( lib, "Urlmon.lib")
   URLDownloadToFile(0, "http://www.abc.com/abc.exe", "c://abc.exe", 0, 0);

   8.复制位图到剪贴板
复制一幅位图到剪贴板相当简单,需要记住的一个是如果位图需要调色盘,
你应当将调色盘也复制。
   函数一:复制设备相关位图到剪贴板
   CopyBitmapToClipboard()函数复制一幅DDB位图到剪贴板,如果提供调色盘它将
同时复制调色盘。注意在最后调用Detach()。这一点很重要,因为此时GDI对象
的物主已经传送至剪贴板。
切换到: 纯代码  
   
   // CopyBitmapToClipboard- Copies a device-dependent bitmap to clipboard
// pWnd- Pointer to window that opens the clipboard
// bitmap- The device-dependent bitmap
// pPal- Pointer to logical palette - Can be NULL
// NOTE- GDI objects are detached from bitmap & pPal
//  as the clipboard owns them after the copy
void CopyBitmapToClipboard( const CWnd *pWnd, CBitmap& bitmap, CPalette* pPal )
{
 ::OpenClipboard(pWnd->GetSafeHwnd());
 ::EmptyClipboard() ;
 if( pPal )
  ::SetClipboardData (CF_PALETTE, pPal->GetSafeHandle() ) ;
 ::SetClipboardData (CF_BITMAP, bitmap.GetSafeHandle() ) ;
 ::CloseClipboard () ;
 bitmap.Detach();
 if( pPal )
  pPal->Detach();
}

函数二:复制设备无关位图到剪贴板
   CopyDIBToClipboard()函数和CopyBitmapToClipboard()非常相似。内存句柄包
含了BITMAPINFO 和位图的位信息,通过GlobalAlloc()分配。
切换到: 纯代码  
   
   // CopyDIBToClipboard- Copies a device-dependent bitmap to clipboard
// pWnd- Pointer to window that opens the clipboard
// hDIB- Memory handle that contains BITMAPINFO & bitmap bits
// pPal- Pointer to logical palette - Can be NULL
// NOTE- GDI objects are detached from bitmap & pPal
//  as the clipboard owns them after the copy
void CopyDIBToClipboard( const CWnd *pWnd, HGLOBAL hDIB, CPalette* pPal )
{
 ::OpenClipboard(pWnd->GetSafeHwnd());
 ::EmptyClipboard();
 if( pPal )
  ::SetClipboardData (CF_PALETTE, pPal->GetSafeHandle() ) ;
 ::SetClipboardData (CF_DIB, hDIB ) ;
 ::CloseClipboard () ;
 bitmap.Detach();
 if( pPal )
  pPal->Detach();
}

   函数三:复制一幅windows图象到剪贴板
切换到: 纯代码  
   
   void CopyWndToClipboard( CWnd *pWnd )
{
 CBitmap bitmap;
 CClientDCdc(pWnd);
 CDC memDC;
 CRectrect;
 
 memDC.CreateCompatibleDC(&dc); 
 
 pWnd->GetWindowRect(rect);
 
 bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
 
 CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
 memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0, 0, SRCCOPY); 
 
 pWnd->OpenClipboard() ;
 EmptyClipboard() ;
 SetClipboardData (CF_BITMAP, bitmap.GetSafeHandle() ) ;
 CloseClipboard () ;
 
 memDC.SelectObject(pOldBitmap);
 bitmap.Detach();
}

使用CopyBitmapToClipboard(this,CBitmap::FromHandle(hBitmap),pPal);
   9.检测internet连线状态
在线返回 TRUE, 掉线返回 FALSE。遍历系统中所有 RAS 连接判断连接情况。Win2K/98 VC6 通过 
切换到: 纯代码  
   
BOOL GetConnectState() 
{ 
 
 LPRASCONN lpRasConn = NULL; 
 DWORD cbBuf = 0; 
 DWORD cConn = 0; 
 DWORD dwRet = 0; 
 HRASCONN hrasconn; 
 RASCONNSTATUS rasStatus; 
 UINT ndx; 
 
 // enumerate connections 
 cbBuf = sizeof(RASCONN); 
 
 if ( (lpRasConn = ( LPRASCONN ) malloc((UINT)cbBuf)) != NULL) 
 { 
  lpRasConn->dwSize = sizeof( RASCONN ); 
  dwRet = RasEnumConnections( lpRasConn, &cbBuf, &cConn ); 
  for (ndx = 0; ndx < cConn; ndx++) 
  { 
   // get to HRASCONN 
   hrasconn = lpRasConn[ndx].hrasconn; 
   // get connection status 
   rasStatus.dwSize = sizeof(RASCONNSTATUS); 
   dwRet = RasGetConnectStatus( hrasconn, &rasStatus ); 
   if ( dwRet ) 
    return 0; 
   else 
   { 
    if (rasStatus.rasconnstate == RASCS_Connected) 
     return 1; 
    else return 0; 
   } 
  } 
 } 
 else 
 { 
  return 0; 
 } 
 
} 


转自 http://blog.csdn.net/y___y/article/details/1537493
(+0)技能Get

建议楼主:搜索关键字 |参考其他资源 |回复 |追问
  error997(error997):   个人中心    课题    目标    代码积累