2010年8月31日

The article is from http://www.java-samples.com/showtutorial.php?tutorialid=591

To set all the bytes in a block of memory to a particular value, use memset(). The function prototype is

void * memset(void *dest, int c, size_t count);

The argument dest points to the block of memory. c is the value to set, and count is the number of bytes, starting at dest, to be set. Note that while c is a type int, it is treated as a type char. In other words, only the low-order byte is used, and you can specify values of c only in the range 0 through 255.

Use memset() to initialize a block of memory to a specified value. Because this function can use only a type char as the initialization value, it is not useful for working with blocks of data types other than type char, except when you want to initialize to 0. In other words, it wouldn't be efficient to use memset() to initialize an array of type int to the value 99, but you could initialize all array elements to the value 0. memset() will be demonstrated in program below.

The memcpy() Function

memcpy() copies bytes of data between memory blocks, sometimes called buffers. This function doesn't care about the type of data being copied--it simply makes an exact byte-for-byte copy. The function prototype is

void *memcpy(void *dest, void *src, size_t count);

The arguments dest and src point to the destination and source memory blocks, respectively. count specifies the number of bytes to be copied. The return value is dest. If the two blocks of memory overlap, the function might not operate properly--some of the data in src might be overwritten before being copied. Use the memmove() function, discussed next, to handle overlapping memory blocks. memcpy() will be demonstrated in program below.

The memmove() Function

memmove() is very much like memcpy(), copying a specified number of bytes from one memory block to another. It's more flexible, however, because it can handle overlapping memory blocks properly. Because memmove() can do everything memcpy() can do with the added flexibility of dealing with overlapping blocks, you rarely, if ever, should have a reason to use memcpy(). The prototype is

void *memmove(void *dest, void *src, size_t count);

dest and src point to the destination and source memory blocks, and count specifies the number of bytes to be copied. The return value is dest. If the blocks overlap, this function ensures that the source data in the overlapped region is copied before being overwritten. Sample program below demonstrates memset(), memcpy(), and memmove().

A demonstration of memset(), memcpy(), and memmove().

1: /* Demonstrating memset(), memcpy(), and memmove(). */
2:
3: #include <stdio.h>
4: #include <string.h>
4:
5: char message1[60] = "Four score and seven years ago ...";
6: char message2[60] = "abcdefghijklmnopqrstuvwxyz";
7: char temp[60];
8:
9: main()
10: {
11:    printf("\nmessage1[] before memset():\t%s", message1);
12:    memset(message1 + 5, `@', 10);
13:    printf("\nmessage1[] after memset():\t%s", message1);
14:
15:    strcpy(temp, message2);
16:    printf("\n\nOriginal message: %s", temp);
17:    memcpy(temp + 4, temp + 16, 10);
18:    printf("\nAfter memcpy() without overlap:\t%s", temp);
19:    strcpy(temp, message2);
20:    memcpy(temp + 6, temp + 4, 10);
21:    printf("\nAfter memcpy() with overlap:\t%s", temp);
22:
23:    strcpy(temp, message2);
24:    printf("\n\nOriginal message: %s", temp);
25:    memmove(temp + 4, temp + 16, 10);
26:    printf("\nAfter memmove() without overlap:\t%s", temp);
27:    strcpy(temp, message2);
28:    memmove(temp + 6, temp + 4, 10);
29:    printf("\nAfter memmove() with overlap:\t%s\n", temp);
30:
31: }
message1[] before memset():     Four score and seven years ago ...
message1[] after memset():      Four @@@@@@@@@@seven years ago ...
Original message: abcdefghijklmnopqrstuvwxyz
After memcpy() without overlap: abcdqrstuvwxyzopqrstuvwxyz
After memcpy() with overlap:    abcdefefefefefefqrstuvwxyz
Original message: abcdefghijklmnopqrstuvwxyz
After memmove() without overlap:        abcdqrstuvwxyzopqrstuvwxyz
After memmove() with overlap:   abcdefefghijklmnqrstuvwxyz

ANALYSIS: The operation of memset() is straightforward. Note how the pointer notation message1 + 5 is used to specify that memset() is to start setting characters at the sixth character in message1[] (remember, arrays are zero-based). As a result, the 6th through 15th characters in message1[] have been changed to @.

When source and destination do not overlap, memcpy() works fine. The 10 characters of temp[] starting at position 17 (the letters q through z) have been copied to positions 5 though 14, where the letters e though n were originally located. If, however, the source and destination overlap, things are different. When the function tries to copy 10 characters starting at position 4 to position 6, an overlap of 8 positions occurs. You might expect the letters e through n to be copied over the letters g through p. Instead, the letters e and f are repeated five times.

If there's no overlap, memmove() works just like memcpy(). With overlap, however, memmove() copies the original source characters to the destination.

posted @ 2010-08-31 11:06 lhking 阅读(520) | 评论 (0)编辑 收藏

2010年6月26日

Vc++6.0项目迁到vs2005 应该注意的问题
www.firnow.com    时间 : 2010-06-06  作者:佚名   编辑:壹枝雪糕 点击:  1176 [ 评论 ]
-
-
综合 资源 电子书 社区   1.如果MessageBox("aa") 报错,将其要改成 MessageBox(_TEXT("aa")).我喜欢用MessageBox来调试程序,尤其是在写脚本时,当你不知道程序有没有执行该条语句,以及执行完该条语句后某个变量的值发生了什么变化,在该条语句前后各加一个MessageBox(str),一目了然了吧 .

2.pow(2,10)   要改成  pow((double)2,10)或pow(2.0,10)

       说明:6.0中用到math.h pow()函數時,有這個原型 double pow(int _X,int _Y) 但如果用VC++ 2005的話,pow()的第一個參數就不能再使用int型態,只能使用float、double、long double,VC++ 2005在編譯時會做type checking,然後就過不了,报error C2668

3.Itoa方法名要改成  _Itoa_s

4.error C2440:“static_cast” 无法从“void (__thiscall CChatManagerDlg::* )(WPARAM,LPARAM)”转换为“LRESULT (__thiscall CWnd::* ),出错处在ON_MESSAGE(WM_SETPLAY,OnSetPlay)

解答:将void CVideoBaseView::OnSetPlay(WPARAM wp,LPARAM lp)  改成LRESULT CVideoBaseView::OnSetPlay(WPARAM wp,LPARAM lp){

LRESULT result = Default();

//你原来的代码

return result;}

5.找不到MFC80D.DLL

解决:“工程属性”->“link”->“manifesto file”->“$(IntDir)\$(TargetFileName).intermediate.manifest” 值     改成    $(IntDir)\$(TargetFileName).manifest

文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/3_program/vc/vc_js/200879/132413.html

posted @ 2010-06-26 09:22 lhking 阅读(374) | 评论 (0)编辑 收藏

2010年6月24日

根据MSDN的描述,采用如下的代码来实现阻止关机,结果发现在有的机器上能够阻止关机,在有的机器上却不能阻止(虽然能看到弹出的MessageBox,但还来不及反应,马上就关机了)。(都是WinXP SP2的机器)

view plaincopy to clipboardprint?
LRESULT CPreventShutdownDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{  
    if (WM_QUERYENDSESSION == message)  
    {  
        AfxMessageBox(_T("呵呵,不许关机!"));  
        return FALSE;  // 根据MSDN,WM_QUERYENDSESSION 返回FALSE则取消关机  
    }  
 
    return CDialog::WindowProc(message, wParam, lParam);  

LRESULT CPreventShutdownDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    if (WM_QUERYENDSESSION == message)
    {
        AfxMessageBox(_T("呵呵,不许关机!"));
        return FALSE;  // 根据MSDN,WM_QUERYENDSESSION 返回FALSE则取消关机
    }

    return CDialog::WindowProc(message, wParam, lParam);
}

是什么原因在某些机器上无法阻止关机呢?

回忆关机时经常遇到的场景:

1. 如果某个进程失去响应,关机的时候会提示"...没有响应,是否结束"

2. 使用记事本修改了文档,在没有保存的情况下进行关机,普通情况会提示是否进行保存。

1这种情况不好模拟,但是2是很好模拟的。于是在不能阻止关机的机器上这样进行测试,发现虽然也弹出了是否保存的对话框,但是还是马上就关机了。

果然,和程序无关,应该是机器设置的问题。于是想到了一个很流行的词:"快速关机"

到网上google了一下,发现快速关机是通过如下的方式实现的:

HKEY-CURRENT-USER\Control Panel\Desktop\AutoEndTasks  值为1表示快速关机

                                                                                        普通情况值为0或这个键值不存在

到不能阻止快速关机的机器上一看,果然这个键值为1.

改为0后再运行程序,就都能阻止关机了。

【结论】阻止关机需要两步才能完美的实现,而不仅仅是MSDN中描述的2)

            1) 在程序中先删除这个键值(HKEY-CURRENT-USER\Control Panel\Desktop\AutoEndTasks)

            2) 处理 WM_QUERYENDSESSION 时返回FALSE

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/skyxie/archive/2009/06/09/4255767.aspx

posted @ 2010-06-24 11:03 lhking 阅读(2575) | 评论 (1)编辑 收藏

2010年6月23日

查看未被delete掉的内存,在输出窗口中查看
1
 #define _CRTDBG_MAP_ALLOC
 2 #include <stdlib.h>
 3 #include <crtdbg.h>
 4 #include <windows.h>
 5 
 6 int wmain(vint argc , wchar_t* args[])
 7 {
 8     // 这里运行程序,并在下面的函数调用之前delete掉所有new的东西
 9     _CrtDumpMemoryLeaks();
10     return 0;
11 }
posted @ 2010-06-23 23:14 lhking 阅读(275) | 评论 (0)编辑 收藏

要实现对一个程序的进程注入,然后对被注入的进程进行控制,首先需要查找到要注入的进程ID。如何获取的进程ID呢?windows提供了一个API只要知道了这个进程里面的一个窗口句柄,就可以找到找到该进程ID。函数形式如下:DWORD GetWindowThreadProcessId(
  HWND hWnd,
  LPDWORD lpdwProcessId
);

那如何获取这个窗口的句柄呢?很自然我们可以想到这么一个函数,函数形式如下:

HWND FindWindowEx(     
    HWND hwndParent,
    HWND hwndChildAfter,
    LPCTSTR lpszClass,
    LPCTSTR lpszWindow
);

hwndParent:指向一个待搜索窗口的父窗。

hwndChildAfter:子窗口的句柄。

lpszClass:窗口的类名。

lpszWindow:窗口的标题名。

例如,本示例工程要找到一个对话框里的编辑框,并对这个编辑框进行注入。查找过程如下:

    HWND hWndChild = NULL;
    while(1)
    {
        // 查找对话框窗口,且这个对话框窗口的标题名为“TestDlg”
        HWND hDlg = FindWindowEx(0, NULL, "#32770", "TestDlg");
        if(hDlg == NULL)
        {
            printf("没有找到该对话框窗口!\n");
            exit(1);
        }
        else
        {
            // 查找这个对话框窗口中的edit控件
            hWndChild = FindWindowEx(hDlg, NULL, "Edit",NULL);
            if(hWndChild != NULL)
            {
                break; // 找到这个编辑框,现在要对这个编辑框进行注入
            }
        }
    }

    // 根据查找出的窗口,查询进程
    DWORD dwQQGameId;
    HANDLE hProcessQQGame;
    if(!GetWindowThreadProcessId(hWndChild,&dwQQGameId))
    {
        printf("Error in GetWindowThreadProcessId():%d\n",GetLastError());
        exit(1);
    }
找到这个进程后,然后就要对这个进程进行注入。但是,你别忘记了,当你在其他进程中获取另外进程的窗口句柄,你是没有办法操作这个句柄的。为什么呢?每个进程都被赋予它自己的虚拟地址空间。对于3 2位进程来说,这个地址空间是4 G B,因
为3 2位指针可以拥有从0 x 0 0 0 0 0 0 0 0至0 x F F F F F F F F之间的任何一个值。这使得一个指针能够拥有4 294 967 296个值中的一个值,它覆盖了一个进程的4 G B虚拟空间的范围。对于6 4位进程来说,这个地址空间是1 6 E B(1 01 8字节),因为6 4位指针可以拥有从0 x 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0至0 x F F F F F F F F F F F F F F F F之间的任何值。这使得一个指针可以拥有18 446 744 073 709 551 616个值中的一个值,它覆盖了一个进程的1 6 E B虚拟空间的范围。这是相当大的一个范围。由于每个进程可以接收它自己的私有的地址空间,因此当进程中的一个线程正在运行时,该线程可以访问只属于它的进程的内存。属于所有其他进程的内存则隐藏着,并且不能被正在运行的线程访问。注意在Windows 2000中,属于操作系统本身的内存也是隐藏的,正在运行的线程无法访问。这意味着线程常常不能访问操作系统的数据。Windows 98中,属于操作系统的内存是不隐藏的,正在运行的线程可以访问。因此,正在运行的线程常常可以访问操作系统的数据,也可以破坏操作系统(从而有可能导致操作系统崩溃)。在Windows 98中,一个进程的线程不可能访问属于另一个进程的内存。前面说过,每个进程有它自己的私有地址空间。进程A可能有一个存放在它的地址空间中的数据结构,地址是0 x 1 2 3 4 5 6 7 8,而进程B则有一个完全不同的数据结构存放在它的地址空间中,地址是0 x 1 2 3 4 5 6 7 8。当进程A中运行的线程访问地址为0 x 1 2 3 4 5 6 7 8的内存时,这些线程访问的是进程A的数据结构。当进程B中运行的线程访问地址为0 x 1 2 3 4 5 6 7 8的内存时,这些线程访问的是进程B的数据结构。进程A中运行的线程不能访问进程B的地址空间中的数据结构,反之亦然。

这样看来,若想对这个窗口句柄进行操作,得想方设法使我们能够进入到原宿主进程中,然后执行我们的操作。这个就好象一个寄生虫想要破坏人体的机能,必须得进入我们的体内,寄生在我们的组织上才能够产生作用。现在关键是如何寄生到宿主中呢?

通常,任何进程都可以通过LoadLibrary动态地加载DLL,但是我们如何强制一个外部进程调用该函数呢?答案是CreateRemoteThread。
让我们先来看看LoadLibrary和FreeLibrary的函数声明:

HINSTANCE LoadLibrary(
  LPCTSTR lpLibFileName   // address of filename of library module
);

BOOL FreeLibrary(
  HMODULE hLibModule      // handle to loaded library module
);

再和CreateRemoteThread的线程过程(thread procedure)ThreadProc比较一下:
DWORD WINAPI ThreadProc(
  LPVOID lpParameter   // thread data
);

    你会发现所有的函数都有同样的调用约定(calling convention)、都接受一个32位的参数并且返回值类型的大小也一样。也就是说,我们可以把LoadLibrary/FreeLibrary的指针作为参数传递给CrateRemoteThread。

    然而,还有两个问题(参考下面对CreateRemoteThread的说明)

    1. 传递给ThreadProc的lpStartAddress 参数必须为远程进程中的线程过程的起始地址。
    2. 如果把ThreadProc的lpParameter参数当做一个普通的32位整数(FreeLibrary把它当做HMODULE)那么没有如何问题,但是如果把它当做一个指针(LoadLibrary把它当做一个char*),它就必须指向远程进程中的内存数据。

    第一个问题其实已经迎刃而解了,因为LoadLibrary和FreeLibrary都是存在于kernel32.dll中的函数,而kernel32可以保证任何“正常”进程中都存在,且其加载地址都是一样的。(参看附录A)于是LoadLibrary/FreeLibrary在任何进程中的地址都是一样的,这就保证了传递给远程进程的指针是个有效的指针。

    第二个问题也很简单:把DLL的文件名(LodLibrary的参数)用WriteProcessMemory复制到远程进程。

    所以,使用CreateRemoteThread和LoadLibrary技术的步骤如下:
    1. 得到远程进程的HANDLE(使用OpenProcess)。
    2. 在远程进程中为DLL文件名分配内存(VirtualAllocEx)。
    3. 把DLL的文件名(全路径)写到分配的内存中(WriteProcessMemory)
    4. 使用CreateRemoteThread和LoadLibrary把你的DLL映射近远程进程。
    5. 等待远程线程结束(WaitForSingleObject),即等待LoadLibrary返回。也就是说当我们的DllMain(是以DLL_PROCESS_ATTACH为参数调用的)返回时远程线程也就立即结束了。
    6. 取回远程线程的结束码(GetExitCodeThtread),即LoadLibrary的返回值――我们DLL加载后的基地址(HMODULE)。
    7. 释放第2步分配的内存(VirtualFreeEx)。
    8. 用CreateRemoteThread和FreeLibrary把DLL从远程进程中卸载。调用时传递第6步取得的HMODULE给FreeLibrary(通过CreateRemoteThread的lpParameter参数)。
    9. 等待线程的结束(WaitSingleObject)。

主要代码如下:

#include<stdio.h>
#include<conio.h>
#include<windows.h>

void main()
{
    HWND hWndChild = NULL;
    while(1)
    {
        // 查找对话框窗口,且这个对话框窗口的标题名为“TestDlg”
        HWND hDlg = FindWindowEx(0, NULL, "#32770", "TestDlg");
        if(hDlg == NULL)
        {
            printf("没有找到该对话框窗口!\n");
            exit(1);
        }
        else
        {
            // 查找这个对话框窗口中的edit控件
            hWndChild = FindWindowEx(hDlg, NULL, "Edit",NULL);
            if(hWndChild != NULL)
            {
                break; // 找到这个编辑框,现在要对这个编辑框进行注入
            }
        }
    }

    // 根据查找出的窗口,查询进程
    DWORD dwQQGameId;
    HANDLE hProcessQQGame;
    if(!GetWindowThreadProcessId(hWndChild,&dwQQGameId))
    {
        printf("Error in GetWindowThreadProcessId():%d\n",GetLastError());
        exit(1);
    }


    HINSTANCE hDll = NULL;
    typedef void(*LP_SET_HEDIT_FUN)(HWND);
    LP_SET_HEDIT_FUN m_SethEdit = NULL;
    char DllPath[MAX_PATH] = "D:\\进程注入测试工程\\Bin\\automessagedll.dll";
    hDll = LoadLibrary(DllPath);
    if(hDll)
    {
        m_SethEdit = (LP_SET_HEDIT_FUN)GetProcAddress(hDll,"SethEdit");
    }
    if(m_SethEdit)
    {
        m_SethEdit(hWndChild);
    }
    else
    {
        printf("Can not load SethEdit in the dll(%d).\n",GetLastError());
    }

 

    if( (hProcessQQGame = OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwQQGameId)) == NULL)
    {
        printf("Error in OpenProcess():%d\n",GetLastError());
        getch();
        exit(1);
    }

    int cb = (1+lstrlen(DllPath))* sizeof(char);

    char* RemoteLibFile = (char*)VirtualAllocEx(hProcessQQGame,NULL,cb,MEM_COMMIT,PAGE_READWRITE);
    if(RemoteLibFile == NULL)
    {
        printf("Error in VirtualAllocEx():%d\n",GetLastError());
        getch();
        exit(1);
    }
    if( (WriteProcessMemory(hProcessQQGame,RemoteLibFile,(LPVOID)DllPath,cb,NULL)) == 0)
    {
        printf("Error in WriteProcessMemory():%d\n",GetLastError());
        getch();
        exit(1);
    }


    PTHREAD_START_ROUTINE pfnStartAddr;
    pfnStartAddr = (PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("Kernel32"),"LoadLibraryA");


    HANDLE hThread = CreateRemoteThread(hProcessQQGame,NULL,0,pfnStartAddr,RemoteLibFile,0,NULL);
    if(hThread == NULL)
    {
        printf("Error in CreateRemoteThread():%d",GetLastError());
        getch();
        exit(1);
    }

    WaitForSingleObject(hThread,INFINITE);


    CloseHandle(hThread);
    VirtualFreeEx(hProcessQQGame,RemoteLibFile,0,MEM_RELEASE);
    CloseHandle(hProcessQQGame);
}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kissyfish/archive/2008/12/07/3462055.aspx

posted @ 2010-06-23 15:33 lhking 阅读(1129) | 评论 (0)编辑 收藏

下载文件要用到操作系统的API函数,下面是一个WINDOWS系统中的实现:

//---------------------------------------------------------------------------
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <wininet.h>
#define MAXBLOCKSIZE 1024
#pragma comment( lib, "wininet.lib" ) ;

void download(const char *Url,const char *save_as)/*将Url指向的地址的文件下载到save_as指向的本地文件*/
{
 byte Temp[MAXBLOCKSIZE];
 ULONG Number = 1;

 FILE *stream;
 HINTERNET hSession = InternetOpen(_T("RookIE/1.0"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
 if (hSession != NULL)
 {
  HINTERNET handle2 = InternetOpenUrl(hSession,_T(Url), NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
  if (handle2 != NULL)
  {


   if( (stream = fopen( save_as, "wb" )) != NULL )
   {
    while (Number > 0)
    {
     InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);

     fwrite(Temp, sizeof (char), Number , stream);
    }
    fclose( stream );
   }

   InternetCloseHandle(handle2);
   handle2 = NULL;
  }
  InternetCloseHandle(hSession);
  hSession = NULL;
 }
}

int main(int argc, char* argv[]){

 download("http://www.xmgpt.org/luke/the%20sound%20of%20science.mp3","c:\\index.html");/*调用示例,下载百度的首页到c:\index.html文件*/
 return 0;
}

 

posted @ 2010-06-23 15:27 lhking 阅读(1864) | 评论 (0)编辑 收藏

方法一:
SetTimer(NULL, 0, 1000, (TIMERPROC)Timer2Proc);

VOID CALLBACK Timer2Proc(
       HWND hWnd, // handle of window for timer messages
       UINT uMsg, // WM_TIMER message
       UINT idEvent, // timer identifier
       DWORD dwTime // current system time
       )
{
 return;
}

方法二:
// DLL中的线程函数可以象这样使用Timer
UINT ThreadProc(LPVOID)
{

 SetTimer(NULL, 1, 5000, NULL);
 MSG msg;
 // PeekMessage 强制系统为该线程建立消息栈
 PeekMessage(&msg, NULL, NULL, NULL, FALSE);
 while (GetMessage(&msg, NULL, NULL, NULL))
 {
  switch (msg.message)
  {
  case WM_TIMER:
   {
    // 这里每5秒钟执行一次
   }
   break;
  }
  //TranslateMessage(&msg);
  //DispatchMessage(&msg);
 }
 KillTimer(NULL, 1);
 return 0;
}

方法三:
创建一个线程, 反复读系统时间不就可以了? 如果定时要求不严,用Sleep就可以了
UINT TimerThread(LPVOID pama)
{
 UINT oldTickCount, newTickCount;
 oldTickCount = GetTickCount();
 while(TRUE)
 {
  while(TRUE)
  {
   newTickCount = GetTickCount();
   if(newTickCount - oldTickCount >= 100)
   {
    oldTickCount = newTickCount;
    break;
   }
  }
  TimeProc();
 }
 return 0;
}
大约每100ms 调用一次TimeProc();

posted @ 2010-06-23 14:57 lhking 阅读(2949) | 评论 (0)编辑 收藏

2010年6月22日

MFC is basicly a library of OO wrapper classes that wrap the Win32 api, and provide objects for the basic window components (windows, buttons, checkboxes etc..). Essentially it is the win32 api objectified.

Also MFC provides some classes that resemble classes found in the STL. As MFC was made before STL was fully standardised.

My knowledge is incomplete. But that is the basic Idea.



 User Rating: 1019   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

The difference between Win32 and MFC are pretty straightforward:

The Windows API (Win32) uses a C interface to access windows functionality. It requires that you write all of your own code to manage windows and message handling. It is quite straightforward but you will need a good reference, like MSDN, and some good tutorials or samples to get started.

In contrast, MFC, which stands for Microsoft Foundation Classes, are a collection of C++ classes which encapsulate the Win32 API. MFC has some wizards which make the initial creation of a program quick and easy, but I have to admit the learning curve of MFC can sometimes be frustrating as Microsoft seems to have done everything in a way that can at times seem counter-intuitive.

Whenever I write an application I write it in MFC but I have been writing applications in MFC for a long time. If all you want is a message loop and a window handle for a game, use Win32. If you want to write a larger application like an editor, maybe MFC is the right tool.

Ideally, I would suggest skipping both Win32 and MFC and writing tools in .NET. I do not have any personal experience in it but people I work with sure can get a lot done using it. It may well be worth investigation.

Best of luck,

- S

 User Rating: 1352   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by Sphet

Whenever I write an application I write it in MFC but I have been writing applications in MFC for a long time. If all you want is a message loop and a window handle for a game, use Win32. If you want to write a larger application like an editor, maybe MFC is the right tool.

- S


I am planning to write an interactive 3D environment does that mean using WIN32 application is a better tool for it?

also is it possible to use openGL for oject creation and DirectXinput for the interactive control?
please give me some suggestion



millions of thanks

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by muimui1911
I am planning to write an interactive 3D environment does that mean using WIN32 application is a better tool for it?

For a game Win32 is usually better.
I think I have also heard that MFC doesn't work well in fullscreen.

Quote:
Original post by muimui1911
also is it possible to use openGL for oject creation and DirectXinput for the interactive control?

You can render with opengl and use directinput for input.

____________________________________________________________
Programmers Resource Central

 User Rating: 1107   |  Rate This User  Send Private MessageView ProfileView GD Showcase Entries Report this Post to a Moderator | Link

what about mouse movement like shooting games?

how can i do that, please give me some direction

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

You can use directinput for mouse movement. Or you can use GetCursorPos(POINT *p);

____________________________________________________________
Programmers Resource Central

 User Rating: 1107   |  Rate This User  Send Private MessageView ProfileView GD Showcase Entries Report this Post to a Moderator | Link

How do i use that? can you give me some example and much clearer direction

millions of thanks

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

msdn
Though if you want a camera class look here and here.
posted @ 2010-06-22 11:43 lhking 阅读(312) | 评论 (0)编辑 收藏

2010年6月21日

对于众多人提出的c/c++中指针难学的问题做个总结:

  指针学习不好关键是概念不清造成的,说的简单点就是书没有认真看,指针的学习犹如人在学习饶口令不多看多学多练是不行的,下面是两个很经典的例子,很多书上都有,对于学习的重点在于理解*x和x的理解,他们并不相同,*x所表示的其实就是变量a本身,x表示的是变量a在内存中的地址,如果想明白可以输出观察cout<<*x"|"x;,当定义了int *x;后对x=&a的理解的问题。仔细阅读和联系下面的两个例子我想指针问题就不是难点了!

#include <stdio.h>

main()
{
int a,b; /* 定义a,b两个整形变量用于输入两个整数 */
int *point_1,*point_2,*temp_point; /* 定义三个指针变量 */
scanf("%d,%d",&a,&b); /* 格式化输入a,b的值 */
point_1=&a; /* 把指针变量point_1的值指向变量a的地址 */
point_2=&b; /* 把指针变量point_2的值指向变量b的地址 */
if (a<b)
{
     temp_point=point_1; /* 这里的temp_point是用于临时存储point_1的值也就是变量a的地址的 */
     point_1=point_2; /* 把point_2的值赋予point_1 */
     point_2=temp_point;
     /* 由于point_1的值已经改变无法找到,利用前面临时存储的也就是temp_point找回原point_1的值赋予point_2,打到把point_1和point_2值对换的目的*/
}
printf("%d,%d",*point_1,*point_2); /* 利用*point_1和*point_2也就是分辨指向b和a的方法把值显示自爱屏幕上 */
}

/* 此题需要注意和了解是的此法并没有改变变量a,b的值只是利用指针变量分别存储a和b的地址,然后再把那两个指针变量的值对换一下其实就是存储在
指针变量里面a与b的地址对换,在利用*point_1和*point_2的方式把调换后的值显示出来这里的*point_1实际就是a,此中算法并非真的改变a,b的值,而是
利用指针进行地址交换达到大小排序的目的.
*/



#include <stdio.h>

main()
{
int a,b; /* 定义a,b两个整形变量用于输入两个整数 */
int *point_1,*point_2; /* 定义三个指针变量 */
scanf("%d,%d",&a,&b); /* 格式化输入a,b的值 */
point_1 = &a; /* 把指针变量point_1的值指向变量a的地址 */
point_2 = &b; /* 把指针变量point_2的值指向变量b的地址 */
compositor(point_1,point_2); /* 调用自定义的排序涵数,把a,b的地址传递给point_1和point_2 */
printf("%d,%d",a,b); /* 打印出a,b的值 */
}

static compositor(p1,p2)
int *p1,*p2; /* 定义形式参数p1,p2为指针变量 */
{
int temp; /* 建立临时存储变量 */
     if (*p1<*p2) /* 如果*p1<p2,注意这里的*p1和*p2其实就是a和b */
     {
         temp = *p1; /* 利用变量temp用于临时存储*p1和就是a的值 */
         *p1 = *p2; /* 将*p1的值也就是a的值换成*p2的值也就是b的值,等价于a=b */
         *p2 = temp; /* 将*p2的值也就是temp的值等价于b=temp */
     }
}

/* 注意:此题与上题不同的是,直接改变了a于b的值达到真实改变的目的 */

posted @ 2010-06-21 23:02 lhking 阅读(219) | 评论 (0)编辑 收藏
VC常用数据类型转换详解
我们先定义一些常见类型变量借以说明
int i = 100;
long l = 2001;
float f=300.2;
double d=12345.119;
char username[]="程佩君";
char temp[200];
char *buf;
CString str;
_variant_t v1;
_bstr_t v2;
一、其它数据类型转换为字符串

短整型(int)
itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制
itoa(i,temp,2); ///按二进制方式转换
长整型(long)
ltoa(l,temp,10);
浮点数(float,double)
用fcvt可以完成转换,这是MSDN中的例子:
int decimal, sign;
char *buffer;
double source = 3.1415926535;
buffer = _fcvt( source, 7, &decimal, &sign );
运行结果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0
decimal表示小数点的位置,sign表示符号:0为正数,1为负数
CString变量
str = "2008北京奥运";
buf = (LPSTR)(LPCTSTR)str;
BSTR变量
BSTR bstrValue = ::SysAllocString(L"程序员");
char * buf = _com_util::ConvertBSTRToString(bstrValue);
SysFreeString(bstrValue);
AfxMessageBox(buf);
delete(buf);
CComBSTR变量
CComBSTR bstrVar("test");
char *buf = _com_util::ConvertBSTRToString(bstrVar.m_str);
AfxMessageBox(buf);
delete(buf);
_bstr_t变量
_bstr_t类型是对BSTR的封装,因为已经重载了=操作符,所以很容易使用
_bstr_t bstrVar("test");
const char *buf = bstrVar;///不要修改buf中的内容
AfxMessageBox(buf);

通用方法(针对非COM数据类型)
用sprintf完成转换
char buffer[200];
char c = '1';
int   i = 35;
long j = 1000;
float f = 1.7320534f;
sprintf( buffer, "%c",c);
sprintf( buffer, "%d",i);
sprintf( buffer, "%d",j);
sprintf( buffer, "%f",f);
二、字符串转换为其它数据类型
strcpy(temp,"123");
短整型(int)
i = atoi(temp);
长整型(long)
l = atol(temp);
浮点(double)
d = atof(temp);
CString变量
CString name = temp;
BSTR变量
BSTR bstrValue = ::SysAllocString(L"程序员");
...///完成对bstrValue的使用
SysFreeString(bstrValue);
CComBSTR变量
CComBSTR类型变量可以直接赋值
CComBSTR bstrVar1("test");
CComBSTR bstrVar2(temp);
_bstr_t变量
_bstr_t类型的变量可以直接赋值
_bstr_t bstrVar1("test");
_bstr_t bstrVar2(temp);

三、其它数据类型转换到CString
使用CString的成员函数Format来转换,例如:

整数(int)
str.Format("%d",i);
浮点数(float)
str.Format("%f",i);
字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值
str = username;
对于Format所不支持的数据类型,可以通过上面所说的关于其它数据类型转化到char *的方法先转到char *,然后赋值给CString变量。
四、BSTR、_bstr_t与CComBSTR

CComBSTR 是ATL对BSTR的封装,_bstr_t是C++对BSTR的封装,BSTR是32位指针,但并不直接指向字串的缓冲区。
char *转换到BSTR可以这样:
BSTR b=_com_util::ConvertStringToBSTR("数据");///使用前需要加上comutil.h和comsupp.lib
SysFreeString(bstrValue);
反之可以使用
char *p=_com_util::ConvertBSTRToString(b);
delete p;
具体可以参考一,二段落里的具体说明。
CComBSTR与_bstr_t对大量的操作符进行了重载,可以直接进行=,!=,==等操作,所以使用非常方便。
特别是_bstr_t,建议大家使用它。

五、VARIANT 、_variant_t 与 COleVariant

VARIANT的结构可以参考头文件VC98\Include\OAIDL.H中关于结构体tagVARIANT的定义。
对于VARIANT变量的赋值:首先给vt成员赋值,指明数据类型,再对联合结构中相同数据类型的变量赋值,举个例子:
VARIANT va;
int a=2001;
va.vt=VT_I4;///指明整型数据
va.lVal=a; ///赋值
对于不马上赋值的VARIANT,最好先用Void VariantInit(VARIANTARG FAR* pvarg);进行初始化,其本质是将vt设置为VT_EMPTY,下表我们列举vt与常用数据的对应关系:
Byte bVal; // VT_UI1.
Short iVal; // VT_I2.
long lVal; // VT_I4.
float fltVal; // VT_R4.
double dblVal; // VT_R8.
VARIANT_BOOL boolVal; // VT_BOOL.
SCODE scode; // VT_ERROR.
CY cyVal; // VT_CY.
DATE date; // VT_DATE.
BSTR bstrVal; // VT_BSTR.
DECIMAL FAR* pdecVal // VT_BYREF|VT_DECIMAL.
IUnknown FAR* punkVal; // VT_UNKNOWN.
IDispatch FAR* pdispVal; // VT_DISPATCH.
SAFEARRAY FAR* parray; // VT_ARRAY|*.
Byte FAR* pbVal; // VT_BYREF|VT_UI1.
short FAR* piVal; // VT_BYREF|VT_I2.
long FAR* plVal; // VT_BYREF|VT_I4.
float FAR* pfltVal; // VT_BYREF|VT_R4.
double FAR* pdblVal; // VT_BYREF|VT_R8.
VARIANT_BOOL FAR* pboolVal; // VT_BYREF|VT_BOOL.
SCODE FAR* pscode; // VT_BYREF|VT_ERROR.
CY FAR* pcyVal; // VT_BYREF|VT_CY.
DATE FAR* pdate; // VT_BYREF|VT_DATE.
BSTR FAR* pbstrVal; // VT_BYREF|VT_BSTR.
IUnknown FAR* FAR* ppunkVal; // VT_BYREF|VT_UNKNOWN.
IDispatch FAR* FAR* ppdispVal; // VT_BYREF|VT_DISPATCH.
SAFEARRAY FAR* FAR* pparray; // VT_ARRAY|*.
VARIANT FAR* pvarVal; // VT_BYREF|VT_VARIANT.
void FAR* byref; // Generic ByRef.
char cVal; // VT_I1.
unsigned short uiVal; // VT_UI2.
unsigned long ulVal; // VT_UI4.
int intVal; // VT_INT.
unsigned int uintVal; // VT_UINT.
char FAR * pcVal; // VT_BYREF|VT_I1.
unsigned short FAR * puiVal; // VT_BYREF|VT_UI2.
unsigned long FAR * pulVal; // VT_BYREF|VT_UI4.
int FAR * pintVal; // VT_BYREF|VT_INT.
unsigned int FAR * puintVal; //VT_BYREF|VT_UINT.

_variant_t是VARIANT的封装类,其赋值可以使用强制类型转换,其构造函数会自动处理这些数据类型。
使用时需加上#include <comdef.h>
例如:
long l=222;
ing i=100;
_variant_t lVal(l);
lVal = (long)i;

COleVariant的使用与_variant_t的方法基本一样,请参考如下例子:
COleVariant v3 = "字符串", v4 = (long)1999;
CString str =(BSTR)v3.pbstrVal;
long i = v4.lVal;

六、其它一些COM数据类型
根据ProgID得到CLSID
HRESULT CLSIDFromProgID( LPCOLESTR lpszProgID,LPCLSID pclsid);
CLSID clsid;
CLSIDFromProgID( L"MAPI.Folder",&clsid);
根据CLSID得到ProgID
WINOLEAPI ProgIDFromCLSID( REFCLSID clsid,LPOLESTR * lplpszProgID);
例如我们已经定义了 CLSID_IApplication,下面的代码得到ProgID
LPOLESTR pProgID = 0;
ProgIDFromCLSID( CLSID_IApplication,&pProgID);
...///可以使用pProgID
CoTaskMemFree(pProgID);//不要忘记释放
七、ANSI与Unicode
Unicode称为宽字符型字串,COM里使用的都是Unicode字符串。
将ANSI转换到Unicode
(1)通过L这个宏来实现,例如: CLSIDFromProgID( L"MAPI.Folder",&clsid);
(2)通过MultiByteToWideChar函数实现转换,例如:
char *szProgID = "MAPI.Folder";
WCHAR szWideProgID[128];
CLSID clsid;
long lLen = MultiByteToWideChar(CP_ACP,0,szProgID,strlen(szProgID),szWideProgID,sizeof(szWideProgID));
szWideProgID[lLen] = '\0';
(3)通过A2W宏来实现,例如:
USES_CONVERSION;
CLSIDFromProgID( A2W(szProgID),&clsid);
将Unicode转换到ANSI
(1)使用WideCharToMultiByte,例如:
// 假设已经有了一个Unicode 串 wszSomeString...
char szANSIString [MAX_PATH];
WideCharToMultiByte ( CP_ACP, WC_COMPOSITECHECK, wszSomeString, -1, szANSIString, sizeof(szANSIString), NULL, NULL );
(2)使用W2A宏来实现,例如:
USES_CONVERSION;
pTemp=W2A(wszSomeString);
八、其它
对消息的处理中我们经常需要将WPARAM或LPARAM等32位数据(DWORD)分解成两个16位数据(WORD),例如:
LPARAM lParam;
WORD loValue = LOWORD(lParam);///取低16位
WORD hiValue = HIWORD(lParam);///取高16位

对于16位的数据(WORD)我们可以用同样的方法分解成高低两个8位数据(BYTE),例如:
WORD wValue;
BYTE loValue = LOBYTE(wValue);///取低8位
BYTE hiValue = HIBYTE(wValue);///取高8位

两个16位数据(WORD)合成32位数据(DWORD,LRESULT,LPARAM,或WPARAM)
LONG MAKELONG( WORD wLow, WORD wHigh );
WPARAM MAKEWPARAM( WORD wLow, WORD wHigh );
LPARAM MAKELPARAM( WORD wLow, WORD wHigh );
LRESULT MAKELRESULT( WORD wLow, WORD wHigh );

两个8位的数据(BYTE)合成16位的数据(WORD)
WORD MAKEWORD( BYTE bLow, BYTE bHigh );

从R(red),G(green),B(blue)三色得到COLORREF类型的颜色值
COLORREF RGB( BYTE byRed,BYTE byGreen,BYTE byBlue );
例如COLORREF bkcolor = RGB(0x22,0x98,0x34);

从COLORREF类型的颜色值得到RGB三个颜色值
BYTE Red = GetRValue(bkcolor); ///得到红颜色
BYTE Green = GetGValue(bkcolor); ///得到绿颜色
BYTE Blue = GetBValue(bkcolor); ///得到兰颜色

九、注意事项
假如需要使用到ConvertBSTRToString此类函数,需要加上头文件comutil.h,并在setting中加入comsupp.lib或者直接加上#pragma comment( lib, "comsupp.lib" )

posted @ 2010-06-21 22:59 lhking 阅读(287) | 评论 (0)编辑 收藏
仅列出标题  下一页

导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜