WindowsPrograming

From DigitalBlacksmith

Jump to: navigation, search

Windows Programming

Contents

[edit] C++

[edit] CTRL-C trapping

http://msdn.microsoft.com/en-us/library/ms685049(VS.85).aspx

[edit] Critical Section

http://www.codeguru.com/cpp/misc/misc/threadsprocesses/article.php/c3843/

[edit] Time Functions

http://www.codersource.net/cpp_date_time.html

  • Useful

http://www.codeproject.com/KB/datetime/datetimedisc.aspx

[edit] Releasing Memory

Handy template class

[edit] Debugging

[edit] Debug with Visual Studio

You can launch an external application that is compiled with debug support (e.g. ARMARSP_debug) and attach a debugger post-launch.

1. Launch the debug exe/dll etc

2. While it's running, open the corresponding project -- attach breakpoints as needed

3. Hit Debug->>Process

Booya!

[edit] Debug Method

Add this method then use debug() in place of printf

Notes:

  • Ensure you include stdio and stdarg. If you forget stdarg it will compile but won't work as advertised


With Windows option:

  • Make a #define with DEBUG_AUGMENTED_CONTROLS to true for all debuging
  • Define WINDOWS_DEBUG to generate Win32 API debug messages (use DebugView to view them):
 
        /**
        *     Debug handler
        *   http://bytes.com/forum/thread212702.html
        */            
        void debug(char *fmt, ...)
        {        
            if(DEBUG_AUGMENTED_CONTROL) {
 
                if(WINDOWS_DEBUG) {
                    char buffer[255];
                    va_list argp;                    
                    va_start(argp, fmt);
                    vsprintf(buffer, fmt, argp);
                    va_end(argp);
                    OutputDebugString(buffer);
                }  else {
                    //standard error
                    va_list argp;
                    fprintf(stderr, "AC: ");
                    va_start(argp, fmt);
                    vfprintf(stderr, fmt, argp);
                    va_end(argp);
                    fprintf(stderr, "\n");
                }
            }
        }
 
 
 
 
#include <stdio.h> 
#include <stdarg.h>
 
/**
*     Debug handler
*   http://bytes.com/forum/thread212702.html
*/
void debug(char *fmt, ...)
{
     va_list argp;
     fprintf(stderr, "CHDEBUG: ");
     va_start(argp, fmt);
     vfprintf(stderr, fmt, argp);
     va_end(argp);
     fprintf(stderr, "\n");
}
 

[edit] Creating a .lib file from DLL without source

http://support.microsoft.com/kb/131313

[edit] Interfaces and Polymorphism

http://www.codeguru.com/cpp/cpp/cpp_mfc/oop/article.php/c9989/

[edit] Java Native Interface (JNI)

Visual Studio Wizard for JNI dll

http://www.javaworld.com/javaworld/jw-10-1999/jw-10-jni.html?page=3

Great Tutorial for JNI and Visual Studio


[edit] Named Pipes

I've been using Windows based named pipes on a single machine without a problem.

When I tried to use them with a second machine on my network -- trouble in paradise!

Here are some links and reading:

http://www.ddj.com/windows/184416624

Networked Named Pipes

MSDN Named Pipes

Believe this is a firewall issue.

[1]

I verified that 445 is open via the File and Print Sharing option described in the KB article above. No Luck.

I turned off the firewall. No Luck.

Grabbed TCPView to sniff packets.

Didn't see anything..could be another problem with paths.

I did some error logging and found out I was getting error 5 - Access Denied.

Found this bit in and excellent thread on CodeGuru:

FiRe_fLy
October 19th, 2001, 01:01 PM
The Error code is:
"5 Access is denied. ERROR_ACCESS_DENIED "

The code for CreateFile is:

m_hInPipe = CreateFile(
m_szPipeName,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

I am looking into the error now. If you know the answer, please help...

Regards,
T Kabilan
October 20th, 2001, 07:00 AM
If client and server run under different user contexts, access denied will happen. This is because of the NULL passed as sec attributes in CreateNamedPipe. A NULL means that only the same user or administrators can connect to it.

Instead of NULL as security attributes, pass a security attributes with a DACL that allows permission for the user running the client program.

The following code sets up the pipe to allow access for everyone.


SECURITY_ATTRIBUTES m_pSecAttrib;
SECURITY_DESCRIPTOR* m_pSecDesc;

m_pSecDesc = (SECURITY_DESCRIPTOR*)LocalAlloc(LPTR,
SECURITY_DESCRIPTOR_MIN_LENGTH);

InitializeSecurityDescriptor(m_pSecDesc,
SECURITY_DESCRIPTOR_REVISION);


SetSecurityDescriptorDacl(m_pSecDesc,TRUE,(PACL)NULL,FALSE))

m_pSecAttrib.nLength = sizeof(SECURITY_ATTRIBUTES);
m_pSecAttrib.bInheritHandle = TRUE;
m_pSecAttrib.lpSecurityDescriptor = m_pSecDesc;

::CreateNamedPipe(PIPE_NAME,
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
32768,
32768,
NMPWAIT_USE_DEFAULT_WAIT,
&m_pSecAttrib);




Best Regards,
Kabilan.

Yup - that was it! Here's my code block for creating the named pip on the server:

   BOOL fConnected; 
   DWORD dwThreadId; 
   HANDLE hPipe, hThread; 
   LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe"); 


   SECURITY_ATTRIBUTES m_pSecAttrib;
	SECURITY_DESCRIPTOR* m_pSecDesc;

	m_pSecDesc = (SECURITY_DESCRIPTOR*)LocalAlloc(LPTR,
	SECURITY_DESCRIPTOR_MIN_LENGTH);

	InitializeSecurityDescriptor(m_pSecDesc,
	SECURITY_DESCRIPTOR_REVISION);


	SetSecurityDescriptorDacl(m_pSecDesc,TRUE,(PACL)NULL,FALSE);

	m_pSecAttrib.nLength = sizeof(SECURITY_ATTRIBUTES);
	m_pSecAttrib.bInheritHandle = TRUE;
	m_pSecAttrib.lpSecurityDescriptor = m_pSecDesc;
 
 
      hPipe = CreateNamedPipe( 
          lpszPipename,             // pipe name 
          PIPE_ACCESS_DUPLEX,       // read/write access 
          PIPE_TYPE_MESSAGE |       // message type pipe 
          PIPE_READMODE_MESSAGE |   // message-read mode 
          PIPE_WAIT,                // blocking mode 
          PIPE_UNLIMITED_INSTANCES, // max. instances  
          BUFSIZE,                  // output buffer size 
          BUFSIZE,                  // input buffer size 
          0,                        // client time-out 
          &m_pSecAttrib);                    // default security attribute 

      if (hPipe == INVALID_HANDLE_VALUE) 
      {
          printf("CreatePipe failed"); 
          return 0;
	  } else {
		  printf("ARMAR Message Server Pipe established"); 
	  }

[edit] TCPIP Sockets

A reusable, high performance, socket server class

  • I have this code in the svn repos as Win32Tools

[edit] Visual Studio 2005

I am having a bunch of trouble compiling the ARMAR code with VS2005.

I pulled down a copy of the Microdoft Platform SDK for Windows Server 2003 R2.

[edit] Linker Errors in Visual Studio

http://support.microsoft.com/default.aspx?scid=kb;en-us;148652


[edit] Uninstalling VS2005

http://msdn2.microsoft.com/en-us/library/ms246604(VS.80).aspx

Personal tools