Quantcast
Channel: CodeProject Latest postings for C / C++ / MFC
Viewing all 3781 articles
Browse latest View live

Complex code in conditionals

$
0
0

I need opinions on code like this:

"if ((++(m_pLF->m_iLogCnt)) >= MSG_QUEUE_FILE_SIZE)"

maybe it's just me and my past - I was taught never to depend on order of operations. This is handled with the parenthesis , but really, should it be that hard to analyze an IF statement?

Charlie GilleyStuck in a dysfunctional matrix from which I must escape...

"Where liberty dwells, there is my country." B. Franklin, 1783 “They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759


Timer interrupt handler on SAM3x8e – Arduino Due

$
0
0
I really do not know where to post this and I   hope I can explain my question , so be gentle. I am using   SAM3x8e   hardware timer (TC3) as an   interrupt. The “TC3_Handler”   (callback?) global function name is “hardcoded” somewhere and fires as expected at desired intervals. I do some verification of this by outputting simple message to LCD. The LCD “class” is declared / instantiated as a global class to the entire application and works amicably. Simple “lcd.print(“OK”)” is executed just fine in the TC3 handler. I also do some hardware output – turn on / off LED – just for an additional indication the handler is being executed. Now I want to execute a method / function of ANOTHER globally defined / instantiated class, again in same TC3 handler. Adding cpal.Run();   did not compile – undefined “cpal”. I had to ADD   extern CPAL cpal; so it would compile and execute cpal.Run(); My question is both LCD and CPAL classes are global, the LCD can be used OK, but the CPAL has to have the extern “reference” added into the TC3 handler. Why? Thanks for reading, appreciate any help. Cheers Vaclav

Tooltip behind dialog

$
0
0
Which could be the reason why a tooltip is layered behind the dialog ?
Here is the implementation:
class CNotifyDlg : public CAlertDialog
{
	CToolTipCtrl m_ToolTip;
}
and
BOOL CNotifyDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
switch(pMsg->message)
	{
		case WM_LBUTTONDOWN:		
		case WM_LBUTTONUP:		
		case WM_MOUSEMOVE:
			m_ToolTip.RelayEvent(pMsg);
	}
 
	return CAlertDialog::PreTranslateMessage(pMsg);
}
BOOL CNotifyDlg::OnInitDialog()
{
	CAlertDialog::OnInitDialog();
 
	m_ToolTip.Create(this);
	m_ToolTip.AddTool(GetDlgItem(IDCANCEL), _T("My message"));
	m_ToolTip.Activate(TRUE);
 
	return TRUE;
}
 
LRESULT CNotifyDlg::OnButtonMouseOver(WPARAM wParam, LPARAM lParam)
{
	if(m_btnClose.GetDlgCtrlID() == (int)wParam)
		m_ToolTip.UpdateTipText(_T("My message again"), GetDlgItem(IDCANCEL));
	m_ToolTip.Update();
 
	return0;
}
and I see the tooltip window, but is just behind the dialog ... I can not figure out why is happen this ...

CAlertDialogis derived from CDialog... what I have done wrong here ? Could you help me ?
Thank you.

Dialog controls not showing up on a child dialog

$
0
0
I am rewriting an old application in VS2010 that was developed in vc++6.0. I am trying to keep the basic structure of the GUI the same which consists of a main Dialog in which I have multiple child dialogs. All the child dialogs except one are simple with no border, title bar or system menu. This works fine in VC++6.0, but on VS2010, none of the controls that I have in those child dialogs show up when I run the program.

Am I missing something simple here that I am not aware of?

thanks
PKNT

C++ reverse engineering

$
0
0
Hi All,
i need C/C++ developers that have skills about reverse engineering. I need to reading custom closed file format, parse the content and fill a defined structure.

if you have experience with reverse engineering and you want more detail about this task please contact me @ luca_2000it@yahoo.it or reply to this message with your private contact.

Thanks
Luca

ManabJB

$
0
0
I like c language...but i know learning 7 programing language

How to read an XML file in C++.?

$
0
0
Hi,

I want to read an xml file in C++. Is there any sample code or any library available.?

I want to read the contents of XML file. Its nodes and attribute's value.
I need an XML parser written in C++.

Any help will be appreciated.

Regards,
Mbatra

URLDownloadToFile stopped working on Windows 10

$
0
0
My app was running fine with Windows 7 & 8.1 using URLDownloadTofile(NULL,sUrl,SFile,0,NULL). With the upgrade to Windows 10 it has stopped working. I've made sure my app has security settings to allow it through the firewall (and this hasn't changed) but now the request to download fails. Also the Visual Studio 2008 Document explorer now just comes up with "Unable to download " which makes me suspect it's a more general problem that just my app. I can't find any security settings that should be blocking my app.

If any one could shed some light on my problem I'd be grateful. I've trawled around the web for a day with no useful results.

Thanks,
David.

Learning C++

$
0
0
I have 10+ years experience with C#. I now have an opportunity learn C++.

I'd like to work in Visual Studio (2012 and higher).

Can someone recommend a decent starter book or web reference?

Thanks
 
If it's not broken, fix it until it is

Checkbox: SetFocus() doesn't draw broken rectangle around it

$
0
0
I am working on a legacy Windows app written in Win32.

There's a checkbox in the screen and i call the SetFocus() function to have the focus on it. I know that it works as far as setting the focus, because now i can use space button in keyboard to check and uncheck the checkbox.

But the problem is, when a control get's the focus, it's supposed show a broken rectangle around it. In my case this broken rectangle is not drawn/shown all the time. Sometimes it shows the broken rectangle and some times it doesn't.

I need to show some visual indication to user that this checkbox has the focus. I tried using WM_CTLCOLORSTATIC, but that creates some painting issues in the window.

So am looking for a simpler solution, by which i could show some visual indicator to user that this checkbox is having focus.

Like changing the text color or background color or even force draw a broken rectangle.

But i have no idea how to do this without using the WM_CTLCOLORSTATIC.

Is there a easier way to show some sort of visual indication on a checkbox in Win32?

Thanks in advance!

First button of toolbar control get activated after another button get executed

$
0
0
Hi Friends,

I am trying to set focus on the first button in a toolbar control if a key combination like ALT + SHIFT + A is pressed. I am using preTranslate Message, like this

if ( pMsg->message == WM_SYSCHAR && (GetKeyState(VK_SHIFT) & 0x8000) && (pMsg->wParam == 'a' || pMsg->wParam == 'A') )
{
     m_wndToolBar.SetFocus();
}

Now. I am getting focus on first button of toolbar. I clicked SPACEBAR and this button get pressed. It open a dialog , after my operation I closed it.

After that, Unfortunately second button get activated. I pressed spacebar again.It will pressed that button and called related function , which will open another dialog. After my operation, I closed it .

Now the focus went to First button. According to project requirement the focus must retain to the last clicked button. I checked in project code, nowhere first button getting activated explicitly.

Please help me out.

Regasrd,
Amrit Agrawal

c++ Qt or C# or JAVA !!

$
0
0
hi i had learn c++ and something about the framework Qt but the problem that the Qt is not used for building apps smartphone like C# or JAVA !!
so do u advice me to leave c++ and Qt and start with C# or JAVA ??
please i need your Advice Thank you
Smile | :)

Windows 10 & WM_QUERY_ENDSESSION/WM_ENDSESSION

$
0
0
Howdy!

With the new great OS -- 10 -- we're seeing something odd.
Running our installer is not shutting down our app.

We do receive the WM_QUERY_ENDSESSION and we do reply with TRUE ( ok to shutdown )
But, upon receiving the WM_ENDSESSION we receive a FALSE (don't shutdown).

Somewhere between the QUERY and the END -- "someone" is saying NO to this.

How does one go about debugging this sort of thing?

I've used SPY++ but am not finding it to be the most useful tool, except to confirm what I knew in code. SPY is not telling me who sends the QUERY. If I could figure out where these messages are coming from, maybe I could watch the conversations taking place around this transaction.

-- in previous versions of Windows this was not an issue -- it worked like we would expect. But Windows10 is telling me not to end my session.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Peter Weyzen
Staff Engineer
soonr.com -- PC Power delivered to your phone

Adjust paper size

$
0
0
I have this code

BOOL CFormOptions::OnPreparePrinting(CPrintInfo* pInfo) 
{
	tagPDA pPrintDlg;
	AfxGetApp()->GetPrinterDeviceDefaults (&pPrintDlg);
	pInfo->m_pPD->m_pd.hDevMode = pPrintDlg.hDevMode;
	LPDEVMODE devMode = pInfo->m_pPD->GetDevMode();
    if (devMode)
    {
	   if (m_legal)
	     devMode->dmPaperSize = 45; // DMPAPER_10X11;  // DMPAPER_LEGAL;
else
         devMode->dmPaperSize = DMPAPER_LEGAL; // _LETTER

	   devMode->dmPaperWidth = 2000;
 

	   //devMode->dmPaperSize = DMPAPER_10X11;

	   GlobalUnlock(devMode);
    }
		
    if ( pInfo->m_bPreview )        // normal print preview
return DoPreparePrinting(pInfo);
 
    return DoPreparePrinting(pInfo);
}

and I can adjust the paper size from letter to legal. But if I try to adjust the paper size to DMPAPER_10X11 or any other size, the software ignores it and just uses letter size.

How can I adjust the paper size to be something like DMPAPER_10X11? I tried adjust the PaperWidth as well and that is ignored as well. So Im not sure what Im doing wrong.

Any help, any direction any one can provide will be greatly appreciated.

CMFCToolBarComboBoxButton::GetByCmd returns NULL

$
0
0
I created a CMFCToolBarComboBoxButton in a CMFCToolBar as following:
afx_msg LRESULT CMainFrame::OnToolbarReset(WPARAM wp, LPARAM lp)
{
    UINT uiToolBarId = (UINT) wp;
    if (uiToolBarId == IDR_TOOLBAR_FILES)
    {
        CMFCToolBarComboBoxButton omboButton1(IDC_SRC_PRO_LIST, GetCmdMgr ()->GetCmdImage(IDC_SRC_PRO_LIST, FALSE), CBS_DROPDOWNLIST);
	
        comboButton1.EnableWindow(FALSE);
        comboButton1.SetCenterVert();
        comboButton1.SetDropDownHeight(200);
        comboButton1.SetFlatMode();
        m_wndFilesBar.ReplaceButton (IDC_SRC_PRO_LIST, comboButton1);
     }
}
It runs well.
But when I want to add some content into it from another thread.I defined a function:
int CMainFrame::CBAddItem(LPCTSTR lpszItem, DWORD_PTR dwData)
{
    int r = -1;
    CMFCToolBarComboBoxButton* pSrcCombo = CMFCToolBarComboBoxButton::GetByCmd (IDC_SRC_PRO_LIST);
    if(pSrcCombo)
    {
        r = pSrcCombo->AddItem(lpszItem, dwData);
    }
    return r;
}
The function CMFCToolBarComboBoxButton::GetByCmd always return NULL. And I traced this function, found:

int__stdcall CMFCToolBar::GetCommandButtons(UINT uiCmd, CObList& listButtons)
{
    .....	
    for (POSITION posTlb = afxAllToolBars.GetHeadPosition(); posTlb != NULL;)
    {
        CMFCToolBar* pToolBar = (CMFCToolBar*) afxAllToolBars.GetNext(posTlb);
        ENSURE(pToolBar != NULL);
 
        if (CWnd::FromHandlePermanent(pToolBar->m_hWnd) != NULL)
CWnd::FromHandlePermanent(pToolBar->m_hWnd) always return NULL.
So why?

How to send a text file from C to java and receive the same text file from java to C

$
0
0
I would like to know how to send a text file from C to java, in which java has to receive the file and send it back to C. I have developed the code, but i am facing error in the receive function
#include
#include
#include
#include
#include
#include

#include
#include
#include
#include
#include
#include
#include
#include

int send_text(int socket)

{
FILE *text;
char a[50];
int size, read_size, stat, packet_index;
char send_buffer[8008], read_buffer[8008];
int wrt=0,sock_fd,tsize=0;
packet_index = 1;
int i=0;
text = fopen("/home/sosdt009/Desktop/character3.txt", "r");
if (text == NULL)
{
printf("Error Opening text File:");
exit(-1);
}

printf("Getting text Size:\n");
gets(a);
fseek(text, 0, SEEK_END);
size = ftell(text);
fseek(text, 0, SEEK_SET);
printf("Total text size: %d \n", size);
gets(a);
 
//Send text Size

printf("Sending text Size:\n",size);
gets(a);
send(socket, (void *)&size, sizeof(size), 0);
 
/*do
 
{
 
    stat = recv(socket, read_buffer, 1024, 0);
 
}*/while(stat < 0)
printf("Socket data:%s \n", read_buffer); 
gets(a);
 
while(size>tsize) 
{
 
    //Read from the file into our send buffer
    read_size = fread(send_buffer,1,sizeof(send_buffer),text);
    printf("The size of send buffer:%c \n",send_buffer);
    gets(a);
    printf("The read size value is :%d \n", read_size);
    gets(a);
 
    //Send data through our socket 
do
    {
 
        stat = send(socket, send_buffer, read_size, 0);
        printf("The send size value is: %d \n", size);
        gets(a);
        printf("The read size value is: %d \n", read_size);
        gets(a);
 
    } while (stat < 0); 
    printf("Packet %d, sent %d bytes.\n", packet_index, read_size);
    gets(a);
 
    //packet_index++;
//Zero out our send buffer

    tsize = tsize+read_size;
    printf("The tsize value is:%d \n",tsize);
    gets(a);
        memset(send_buffer,0, sizeof(send_buffer));
 
    if(read_size<=NULL)
    {
        printf("The connection is transferred to received text: \n");
        gets(a);
    }
}
fclose(text);
//close(socket);
printf("Text successfully send:\n");
gets(a);
return0;   

}

int receive_text(int socket)
{
int buffersize = 77,recv_size=0,read_size = 1, write_size,size;
char *pBuf,a[50],b[77];
int errnom,i;
FILE *textnew;
size_t rec;
textnew = fopen("/home/sosdt009/Desktop/receivednew.txt", "a");

if (textnew == NULL)

{

printf("Error has occurred, text file could not be opened \n");
return -1;
}
 
//Loop while we have not received the entire file yet
while(read_size > 0)
{
 
        //ioctl(socket, FIONREAD, &buffersize);
        printf("The Buffersize is    :%d\n",buffersize);
        gets(a);
        printf("The size of socket is:%d\n",socket);
        gets(a);
 
    //We check to see if there is data to be read from the socket 
if (buffersize > 0)
    {
        printf("Buffersize value is  :%d\n", buffersize);
        gets(a);
        pBuf = malloc(sizeof(b));
        if (!pBuf)
        {
            printf(errnom, "Memory Error Cannot Allocate!\n");
            gets(a);
            exit(-1);
        }
 
        //read_size = read(socket,pBuf,buffersize);
//read_size = fread(pBuf,buffersize,1,textnew);

        read_size = recv(socket,pBuf,sizeof(pBuf),1);
        //memset(pBuf,'\0',sizeof(pBuf));
        printf("Read size value is :%d \n",read_size);
        gets(a);
        printf("Buffersize value is:%d \n",sizeof(pBuf));
        gets(a);
 
        /*if (read_size < 0)
        {
            printf("%d\n",strerror(errno));
            printf("Data not written to the file:\n");
            gets(a);
            goto free;
        }*/ 
        //Write the currently read data into our text file
//write_size = write(textnew,read_size,pBuf);                //using write function
        write_size = fwrite(pBuf,1,sizeof(pBuf),textnew); 
        free(pBuf);
        printf("Write size value is   :%d \n",write_size);
        gets(a);
        printf("Buffer size value is  :%d \n",sizeof(pBuf));
        gets(a);            
 
        //Increment the total number of bytes read

        recv_size += read_size;
        printf("Received size value is:%d \n",recv_size);
        gets(a);
        printf("Read size value is    :%d \n",read_size);       
        gets(a);    
    }
}
free:
fclose(textnew);
close(socket);  
printf("Text Successfully Received:\n");
gets(a);
return0;

}

int main(int argc,char *argv[])
{
int socket_desc;
struct sockaddr_in server;
char *parray,errnomu;
 
//Create socket

socket_desc = socket(AF_INET,SOCK_STREAM,0);
if(socket_desc == -1)
{
      printf("Could not create socket \n");
}
      memset(&server,0,sizeof(server));
      server.sin_addr.s_addr = inet_addr("10.170.0.38");
      server.sin_family = AF_INET;
      server.sin_port = htons(6999);
 
//Connect to remote server
if (connect(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
      {
 
         printf(strerror(errnomu));
 
         printf("Connect Error \n");
             return -1;
 
          }
 
     puts("Connected");
     send_text(socket_desc);
     receive_text(socket_desc);
     close(socket_desc);
     return0;

}

Using #define wrong way ?

$
0
0
I am trying to build SAM3x8E registers database.
I though I could use serial of #define(s) to do that but run into a problem

In this sample I can build PIO_SODR but I cannot add another (port) offset to it - see PIOA_SODR.

I think the problem is my defines are not really int (32 bits) , but only symbols.
I did compare the (symbol) PIO_SODR to its true hex value and it failed.

BUT - I can add the first offset of 0x30 no problem. Ether plain add (+) or (|) or works.

Any help would be as always appreciated.

/* MSD upper 16 bits byte base*/#define SAM_MSB  0x400E               //MSD upper 16 bits byte base
/* LSD lower 16 bits byte base */#define SAM_LSB  0xE00   
/* PIO offset from PIO A and up */#define PIO_OFFSET        0x200  
/* status ouput data register */#define PIO_SODR  SAM_MSB << 16 | SAM_LSB + 0x30       // base plus reg offset OK 
#define PIOA_SODR PIO_SODR + 0x200  // add in another offset fails

Accessing struct member and getting error I do not know how to fix it.

$
0
0
I am stuck again.
I have inherited this piece of code which basically associate SAM3X8E pins with hardware registers.
I think it is pretty clever usage of array of struct and it works fine.
The original code did not include convenient way to access ALL of the registers, so I am trying to build the missing registers from basis assignments.

The registers are related to "port" ( designated PIOA.. thru PIOF) so if I can get the port assigned to a pin I can build all of the missing registers.  

Here is the code to get the the port info from first (test) pin:
P_DB is a pointer to pins array.
  
( I have left all of the support code here FYI )
  
            ....
            regtype TEST = *P_DB.pPort;         *p_DB[0] fails same way
            //lcd_i2c.print( (int) P_DB[0].pPort, HEX); // test access first pin
            ...

Now here is the error message from compiler ( on TEST) and I have no idea how to fix it:

C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build6265730787782115322.tmp\sketch\AAA_TFT_LCD.h: In member function 'void TFT_LCD::LCD_Writ_Bus(char, char, byte)':
C:\DOCUME~1\Vaclav\LOCALS~1\Temp\build6265730787782115322.tmp\sketch\AAA_TFT_LCD.h:4370:30: error: request for member 'pPort' in '((TFT_LCD*)this)->TFT_LCD::P_DB', which is of non-class type 'volatile uint32_t* [16] {aka volatile long unsigned int* [16]}'
            regtype TEST = *P_DB.pPort;

The error on (int) P_DB[0].pPort, code is pretty much same.

I hope I have provided all of the info necessary so someone can suggest a solution on how to access the pPort member.

      /* Types used for the tables below */
      // theae are indexes to array - what a trick
      //typedef struct _PinDescription
      //{
      //   Pio* pPort ;
      //   uint32_t ulPin ;
      //   uint32_t ulPeripheralId ;
      //   EPioType ulPinType ;
      //   uint32_t ulPinConfiguration ;
      //   uint32_t ulPinAttribute ;
      //   EAnalogChannel ulAnalogChannel ; /* Analog pin in the Arduino context (label on the board) */
      //   EAnalogChannel ulADCChannelNumber ; /* ADC Channel number in the SAM device */
      //   EPWMChannel ulPWMChannel ;
      //   ETCChannel ulTCChannel ;
      //} PinDescription ;
      //
      ///* Pins table to be instanciated into variant.cpp */
      //extern const PinDescription g_APinDescription[] ;

      //// Number of pins defined in PinDescription array
      //#define PINS_COUNT               (79u)
      //#define NUM_DIGITAL_PINS      (54u)
      //#define NUM_ANALOG_INPUTS      (12u)
      //
      //#define digitalPinToPort(P)            ( g_APinDescription[P].pPort )         so what is this refernce to .pPort
      //#define digitalPinToBitMask(P)      ( g_APinDescription[P].ulPin )
      ////#define analogInPinToBit(P)            ( )
      //#define portOutputRegister(port)   ( &(port->PIO_ODSR) )
      //#define portInputRegister(port)      ( &(port->PIO_PDSR) )
      //#define digitalPinHasPWM(P)            ( g_APinDescription[P].ulPWMChannel != NOT_ON_PWM || g_APinDescription[P].ulTCChannel != NOT_ON_TIMER )
      //
      ///*
      // * Pins descriptions
      // * global array of all pins descriptions   as what type
      // *   Arduino/hardware/arduino/sam/variants/arduino_due_x/variant.cpp
      //
      // */
      //extern const PinDescription g_APinDescription[]=
      //{
      //   // 0 .. 53 - Digital pins
      //   // ----------------------
      //   // 0/1 - UART (Serial)
      //   { PIOA, PIO_PA8A_URXD,      ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT,   PIN_ATTR_DIGITAL,                        NO_ADC, NO_ADC, NOT_ON_PWM,   NOT_ON_TIMER }, // URXD
      //   { PIOA, PIO_PA9A_UTXD,      ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT,   PIN_ATTR_DIGITAL,                        NO_ADC, NO_ADC, NOT_ON_PWM,   NOT_ON_TIMER }, // UTXD
      //
      //   // 2
<a href=""></a>[<a href="" target="_blank">^</a>]

MFC Project Style "Visual Studio", but Single Document with multiple views

$
0
0
Hi all,

I am starting to create a framework/IDE for special type of project files.
The GUI like Visual Studio would be perfect for my needs and it can be easily created wih assistance from MFC Application Wizard (Options: "MFC" + "Multiple Documents" + "Doc/View Architecture" + "Visual Studio Project Style")

But this solves only the "surface"...internally I need another structure...I have only one document, but I need to create a bunch of views for the document.
In the "File Explorer" view there should be viewed the structure of the document and in the child view area there should be viewed all views (like the "Tabbed Documents"), related to the document.

But I have absolutely no idea, how to solve this!

Are the any examples, I could use as templates? Or are there any general ideas, how to solve?

Many Thanks

Richard

Can someone take a look at this project, getting linker errors, I've done everything to fix

$
0
0
Basically code is from previous project my tutor helped me develop unfortunately I can no longer contact him, but I'm working on similar project which I did a simple object that moves around on screen, I pasted a pre-existing Direct X tool kit that my teacher gave me and imported it in as a project and did the linker with my other project but it's giving me the linker errors. Please kindly fix this there's no coding errors, also if possible please explain with screenshots what was wrong so I would remember for next time, thank you.
http://www.megafileupload.com/9c1m/P2BallGamev2.zip
Viewing all 3781 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>