Labels:


A smart pointer is a class that wraps a "bare" C++ pointer, to manage the lifetime of the object being pointed to.


With "bare" C++ pointers, the programmer has to explicitly destroy the object when it is no longer useful.

Labels:



int numCPU;
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );

numCPU = sysinfo.dwNumberOfProcessors;

Labels:


There are a number of reasons, which can be broadly classified as business reasons and technical reasons.

Lets start with the business reasons.

  1. C# is an open, public standard (ECMA-334) but .Net is not. Note that .NET Framework which is not covered by the ECMA/ISO standard, includes Windows Forms, ADO.NET, and ASP.NET. Patents that Microsoft holds in these areas may deter non-Microsoft implementations of the full framework. Would you develop a product based on a platform your competitor controls, especially when the product you are developing challenges the same competitor's product?
  2. Suppose you want to develop a new browser and you have a fairly decent operating budget. What people would you want to recruit? Clearly someone with browser development experience. You would try to get the best of the Mozilla/Firefox folk, maybe steal some from IE. Guess what these guys have been developing in. C++.

Now for the technical reasons.

  1. There is no Java/C# counterpart for WebKit. At least not with the same maturity.
  2. They want to leverage their V8 engine. Look into this, this is killer stuff.
  3. The following is a list of all that they are potentially using (this is from Chromium so may not exactly acurate). Can you find mature, Java/C# counterparts for them?
    bsdiff, bspatch, bzip2, dtoa, hunspell, ICU, JSCRE, libjpeg, libpng, libxml, libxslt, LZMA SDK, modp_b64, Mozilla interface to Java Plugin APIs, npapi, nspr, nss, Pthreads for win32, sqlite, tlslite, V8 assembler, WebKit, WTL, zlib
  4. No matter what people say sometimes the ability to operate close to the machine and have a fine grained control over memory matters. This is such a case.
  5. Can you name one significant desktop application done in Java/C#. Eclipse. What else? Maybe this is possible but don't you want to play it safe? Develop in a language that has been really battle tested in this regard.

Labels: ,

For Creating Process in Visual C++ , you can use the function CreateProcess().



CreateProcess:


Creates a new process and its primary thread. The new process runs in the security context of the calling process.

If the calling process is impersonating another user, the new process uses the token for the calling process, not the impersonation token. To run the new process in the security context of the user represented by the impersonation token, use the CreateProcessAsUser or CreateProcessWithLogonW function.

Labels:


No!

It is perfectly legal, moral, and wholesome to use malloc() and delete in the
same program, or to use new and free() in the same program.  But it is illegal,
immoral, and despicable to call free() with a pointer allocated via new, or to
call delete on a pointer allocated via malloc().

Beware! I occasionally get e-mail from people telling me that it works OK for
them on machine X and compiler Y.  That does not make it right! Sometimes
people say, "But I'm just working with an array of char." Nonetheless do not
mix malloc() and delete on the same pointer, or new and free() on the same
pointer! If you allocated via p = new char[n], you must use delete[] p; you
must not use free(p).  Or if you allocated via p = malloc(n), you must use
free(p); you must not use delete[] p or delete p! Mixing these up could cause a
catastrophic failure at runtime if the code was ported to a new machine, a new
compiler, or even a new version of the same compiler.

You have been warned.

Labels:

Posting or sending a user-defined message to the main thread is the best way to trigger something in the main thread from other threads. You start by defining the user-defined message itself. WM_APP is a windows constant that is higher than any message used by windows, so define your own windows message like this:

    #define MY_WM_MESSAGE1  (WM_APP + 1)
Your secondary thread will need an hwnd (not a CWnd*) to post to. You can pass the main hwnd when you create the thread. Then you are ready to send or post a message from the secondary thread:

    ::PostMessage(hwnd, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
  or, 
    ::SendMessage(hwnd, MY_WM_MESSAGE1, (WPARAM)0, (LPARAM)0);
The lParam and wParam can be used to pass any parameters you like to the destination. PostMessage puts the message in the main thread's queue and returns quickly. The main thread processes it asynchronously. SendMessage does not return until the main thread actually gets and processes the message, so it provides interthread synchronization.
To receive and process the message in the main thread code, first declare your message hander function in the main window's h file. MFC does not route user-defined messages to views or docs, so the handler must be a member function of the CMainFrame class (or the main dialog class in a dialog-based app). The handler must have exactly this prototype:

    afx_msg LRESULT OnMyMessage1(UINT wParam, LONG lParam);
The next step is to enter your message handler into the message map, typing it in manually instead of using the wizard. Put in an ON_MESSAGE macro like the following after the "//}}AFX_MSG_MAP" line and before the "END_MESSAGE_MAP()" line.

        //}}AFX_MSG_MAP
        ON_MESSAGE(MY_WM_MESSAGE1, OnMyMessage1)
    END_MESSAGE_MAP()
This adds your message and function into the message mapping table. When the message comes in to the main window your function will be called:

    LRESULT CMainFrame::OnMyMessage1(UINT wParam, LONG lParam)
    {
        ... do something about the message from secondary thread
        return 0; // I handled this message
    }

The method alters the array. No copy is made. Take a look at this example:

void method(char *buff2)
{
  strcpy(buff2,"CHANGHED!!!");
}
int main(int argc, char* argv[])
{
  char buff[512];
  strcpy(buff,"Hello there!");
  printf(buff);
  method(buff);
  printf(buff); //doesn't say "Hello there!" anymore! Says "CHANGHED!!!"
  return 0;
}

\n newline == endl
\r carriage return
\t tabulation
\v vertical tabulation
\b backspace
\f page feed
\a alert (beep)
\' single quotes (')
\" double quotes (")
\? question (?)
\\ inverted slash (\)
\0 null char means end of string
\ - Allows you to continue your string onto the next line. ex. "asdfg\
... hjkl" produces no compile errors.

Using a character array

ASCII
itoa(integer,chararray,10); //integer to string array, 10 = radix, returns the string array

TCHAR
_itot(integer,chararray,10);

Unicode
_itow(integer,chararray,10);


Using wsprintf
wsprintf(buff,"Value: %d", integer)
params are TCHAR *, so Unicode or ASCII may be used
returns "number of characters stored in the output buffer, not counting the terminating null character."
%f =float
%s=string
%c=char
%d=integer



Using the MFC CString:


CString MFCString;
MFCString.Format("Value: %d", integer);
%f =float
%s=string
%c=char
%d=integer

//is the app already running?
::CreateMutex( NULL, TRUE, TEXT("NAME-088FA2340-B10234-12343-B2346-0070967"));
if(GetLastError() == ERROR_ALREADY_EXISTS)
  exit(0);



How do I open a file so it runs in Paint, Word, etc., (but shows a security message if it is an exe)?

ShellExecute(NULL, NULL, strFile, NULL, NULL, SW_SHOW);

How do I open a folder using My Computer?

ShellExecute(NULL, NULL, <fully_qualified_path_to_folder>, NULL, NULL, SW_SHOWNORMAL);

Labels:



The best way to execute a program (in Windows) is to use this code:

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
if(CreateProcess("E:\\info.exe", 0, 0, 0, FALSE, 0, 0, 0, &si, &pi))
{
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
}

Labels:

               This Post will show how to access pointer to a various objects in the Document - View Architecture.