1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

OutputDebugString with variable number of arguments

Discussion in 'MFC' started by shabbir, Jun 8, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Most often a question comes in my mind that why OutputDebugString does not take variable no of arguments like the TRACE or printf function. I dont know the reason why but I have a solution to the problem.

    This article is for those OutputDebugString fan's who are annoyed to use the CString and format the string with the variables like
    Code:
    CString OutMsg;
    OutMsg.Format("FUNCTION NAME >> VARIABLE NAME = %d",VAR_NAME);
    OutputDebugString(OutMsg);
    Here is the substituted function

    Code:
    void _OutputDebugString(LPCTSTR lpOutputString, ... )
    {
    	
    	va_list argptr;    
    	
    	va_start( argptr, lpOutputString );            
    	
    	TCHAR OutMsg[MAX_LENGTH];
    	TCHAR format[MAX_LENGTH];
    	
    	for(int i=0,j=0;lpOutputString[i] != '\0';i++) 
    	{
    		format[j++] = lpOutputString[i];
    		// If escape character
    		if(lpOutputString[i] == '\\')
    		{
    			i++;
    			continue;
    		}
    		// if not a substitutal character
    		if(lpOutputString[i] != '%')
    			continue;
    		
    		format[j++] = lpOutputString[++i];
    		format[j] = '\0';
    		switch(lpOutputString[i])
    		{
    			// string
    		case 's':
    			{
    				char* s = va_arg( argptr, char * );
    				sprintf(OutMsg,format,s);
    				strcpy(format,OutMsg);
    				j = strlen(format);
    				strcat(format," ");
    				break;
    			}
    			// character
    		case 'c':
    			{
    				char c = (char) va_arg( argptr, int );
    				sprintf(OutMsg,format,c);
    				strcpy(format,OutMsg);
    				j = strlen(format);
    				strcat(format," ");
    				break;
    			}
    			// integer
    		case 'd':
    			{
    				int d = va_arg( argptr, int );
    				sprintf(OutMsg,format,d);
    				strcpy(format,OutMsg);
    				j = strlen(format);
    				strcat(format," ");
    				break;
    			}
    		}
    	}           
    	OutputDebugString(OutMsg);
    	va_end( argptr );
    }
    
    Download the cpp file and see how it works.

    I have taken care of escape character \, string %s, integers %d and characters %c but you can add the floating point numbers and many more.
     

    Attached Files:

  2. TEJUS

    TEJUS New Member

    VER NICE,YOU ARE A VERY CLEVER PERSON,I'M ALSO THANKING U FOR REPLYING

    TO MY ARTICLE 'FRIENDSHIP CALCULATOR & HANGMAN'.MY NAME IS TEJUS.

    I WAS VERY SAD BECAUSE NOONE DIDN'T REPLY TO ME TILL UR REPLY.

    I AM STUDYING IN 'VI'(6TH)'IN S.N.PUBLIC SCHOOL ,KOLLAM
     
  3. shabbir

    shabbir Administrator Staff Member

    I could not get you which reply you are talking about.
     
  4. TEJUS

    TEJUS New Member

    just leave it.
     
  5. BlasterBlang

    BlasterBlang New Member

    Thanks for the code. but it doesnt find helping in trace.
     
  6. BlasterBlang

    BlasterBlang New Member

    but neway ur code have provided much knowledge to reslove problem.
     
    Last edited by a moderator: Nov 14, 2006
  7. shabbir

    shabbir Administrator Staff Member

    BlasterBlang, Please confine links to signatures only
     
  8. aisha.ansari84

    aisha.ansari84 New Member

    i m not so clear with it
     
  9. rahul.mca2001

    rahul.mca2001 New Member

    not clear
     
  10. shabbir

    shabbir Administrator Staff Member

    Can you clarify what is not clear?
     
  11. asadullah.ansari

    asadullah.ansari TechCake

  12. shahata

    shahata New Member

    A much more simple approach:

    void _OutputDebugString(LPCTSTR lpOutputString, ... )
    {
    TCHAR OutMsg[MAX_LENGTH];

    va_list argptr;
    va_start(argptr, lpOutputString);

    wvsprintf(OutMsg, lpOutputString, argptr);
    OutputDebugString(OutMsg);

    va_end(argptr);​
    }
     
  13. hsm

    hsm New Member

    The Download ZIP file is corrupt
     
  14. shabbir

    shabbir Administrator Staff Member

    I downloaded once again and they worked fine for me
     

Share This Page