Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
K

keret

@keret
About
Posts
12
Topics
4
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How to access a local postgres database from VC++ with ODBC?
    K keret

    I try to access a local postgres database (actually it is a PokerTracker3 database) from Visual C++ 2008, because I'd like to do a lot of regular analysis, which takes hours of work with PT3, but if I write a standalone program, it can make it for me by a push of a button. I am a slightly advanced C++ programmer, but I'm new to databases. I tried at first with libpqxx, but I couldn't even compile it's test programs. It had configuration problems I couldn't solve. Then I found out about ODBC, and I try this for now. So far I updated postgres to 8.3.9 with psqlodbc 8.4 (that's what was automatically installed with it). I found an example on the net with an access database here: http://www.dmcmsp.com/newHome/odbc/cplusplus/index.htm I tried to amend it to work with postgreSQL. At first, I tried to access the lookup_hand_groups table. Server name is postgres, database name is PT3_2010, username is postgres, password is dbpass. It's a console application (later I will write the reports to a csv file), that's what I have so far:

    #include "stdafx.h"
    #include "windows.h"
    #include <sqlext.h>

    int main(int argc, char\* argv\[\])
    {
    printf("Starting\\r\\n");
    
    SQLHANDLE hEnv; // ODBC Environment handle
    SQLHANDLE hDbc; // ODBC Connection handle
    SQLHANDLE hStmt; // ODBC Statement handle
    
    SQLRETURN status; // SQL return variable to test success or failure
    
    // 1. Create Environment Handle
    status = SQLAllocHandle(SQL\_HANDLE\_ENV, SQL\_NULL\_HANDLE, &hEnv);
    
    // 2. Set the ODBC Version we are using to 3.0
    status = SQLSetEnvAttr(hEnv, SQL\_ATTR\_ODBC\_VERSION, (void \*) SQL\_OV\_ODBC3, 0);
    
    // 3. Create a Connection Handle.
    status = SQLAllocHandle(SQL\_HANDLE\_DBC, hEnv, &hDbc);
    
    // 4. Set Connection Login Timeout to 5 seconds.
    status = SQLSetConnectAttr(hDbc, SQL\_LOGIN\_TIMEOUT,(void \*) 5, 0);
    
    // 5. Open a Connection To Datasource (DSN
    status = SQLConnect(hDbc,(SQLWCHAR \*) "postgres", SQL\_NTS,
    (SQLWCHAR \*) "postgres", SQL\_NTS,
    (SQLWCHAR \*) "dbpass", SQL\_NTS); // connect to DSN
    
    // 6. Create SQL Statement Handle.
    status = SQLAllocHandle(SQL\_HANDLE\_STMT, hDbc, &hStmt);
    
    // 7. Set Cursor Type to Static.
    status = SQLSetStmtAttr(hStmt, SQL\_ATTR\_CURSOR\_TYPE, (SQLPOINTER) SQL\_CURSOR\_STATIC, 0);
    
    // 8. Execute a SQL Statement. Here we get the employee records from Northwind database.
    status = SQLExecDirect(hStm
    
    Database database c++ postgresql tutorial workspace

  • How to delete handles of a thread created with AfxBeginThread
    K keret

    Thank You, I really appreciate your help. This was a complicated problem (for me now at least). The 160B object was another struct I didn't include in the code above.

    C / C++ / MFC c++ question performance tutorial

  • How to delete handles of a thread created with AfxBeginThread
    K keret

    Sorry, I have never used extern before. I put this in MyDlg.h:

    public:
    LONG letsExitNow

    I put this in CMyDlg::OnInitDialog:

    InterlockedExchange(&letsExitNow, 0);

    And I put this in ThreadFunction:

    extern LONG letsExitNow;

    But I got an "unresolved external symbol "long letsExitNow"" error. Did I something wrong?

    C / C++ / MFC c++ question performance tutorial

  • How to delete handles of a thread created with AfxBeginThread
    K keret

    My TreadFunction is in a separate file (MyThread.cpp). How can I reference letsExitNow from there? Or should I put the code in MyDlg.cpp?

    C / C++ / MFC c++ question performance tutorial

  • How to delete handles of a thread created with AfxBeginThread
    K keret

    Thanks! 1. This didn't compile, error was: error C2664: 'ThreadInfo::ThreadInfo(const ThreadInfo &)' : cannot convert parameter 1 from 'ThreadInfo *' to 'const ThreadInfo &' but I used the following:

    ThreadInfo *pInfo = reinterpret_cast (pParam);
    ThreadInfo info = *pInfo;
    delete pInfo;

    And it seems to work. The 748B and 160B blocks are eliminated. 2. I had a threads vector earlier, but I didn't use it. Now I use it as you recommended, but I use ON_WM_DESTROY (I switched off the close button):

    void CFredoDlg::OnDestroy()
    {
    //delete threads;
    for(vector::size_type i = 0; i < threads.size(); i++)
    {
    threads.erase(threads.begin() + i);
    }

    }

    But the 68B and 46B blocks are still there. Now I will try to find a way to terminate the threads. If you have a solution for that, don't keep it!

    C / C++ / MFC c++ question performance tutorial

  • How to delete handles of a thread created with AfxBeginThread
    K keret

    I have an MFC dialog application. It monitors certain types of windows and collects information on them. I start a new thread for every window based on it's title. I use a timer to find windows that meet the criteria. I created a struct named ThreadInfo and I pass it to the thread. At the end I either close the monitored window(s) or close my dialogs. This causes several memory leaks. Here is the code in the timer if it finds a window that meets the criteria:

    //Create ThreadInfo
    ThreadInfo *pInfo = new ThreadInfo;

    //put the information into pInfo
    pInfo->hMainWnd = dlg->GetSafeHwnd();
    pInfo->hMonitoredWnd = hwnd;
    pInfo->x = y; //And so on...
    
    //Start thread
    CWinThread \*pThread = AfxBeginThread(ThreadFunction, (PVOID) pInfo, THREAD\_PRIORITY\_NORMAL, 0, 0);
    

    Here is the code for ThreadFunction:

    UINT ThreadFunction(LPVOID pParam)
    {
    ThreadInfo *pInfo = reinterpret_cast (pParam);

    while (IsWindow(pInfo->hMonitoredWindow))
    {
      
       do.Something;
       Sleep(100);
       
    }
    //delete pInfo;
    return 0;
    

    }

    I tried the following scenarions to eliminate or at least identify the memory leaks. The resulting leaks are below them: A: - I create pInfo and close the dialog first: thrcore.cpp 68B client block mydlg.cpp 748B normal block strcore.cpp 46B normal block B: - I create pInfo and close the monitored window first: mydlg.cpp 748B nromal block strcore.cpp 46B normal block C: - I create pInfo, but don't start the thread: mydlg.cpp 748B normal block strcore.cpp 46B normal block D: - I don't create pInfo and pass NULL to the thread: no leaks! E: - I delete pInfo in ThreadFunction and close monitored window first: no cpp file, 160B normal block F: - I delete pInfo in ThreadFunction and close dialog first: no cpp file, 160B normal block thrcore.cpp 68B client block mydlg.cpp 748B normal block strcore.cpp 46B normal block So I think the 748B and 46B blocks are for pInfo, while the 68B and 160B block are for pThread. But how and where should I delete them? I think I should delete them in the dialog, but how do I send a message from the thread when it ends? And how I delete the handles if I close the dialog first?

    C / C++ / MFC c++ question performance tutorial

  • Strncmp and GetwindowText problem
    K keret

    I had an other searchstring originally. :)

    C / C++ / MFC help question data-structures

  • Strncmp and GetwindowText problem
    K keret

    Thanks guys, you both helped me a lot. At the moment I just cut & paste any code I find, but this will change I hope.

    C / C++ / MFC help question data-structures

  • Strncmp and GetwindowText problem
    K keret

    I enumerate all open windows with Enumwindows. In the processor function I try to check if the title of the window contains a certain string. If yes, put the handle in a list of handles(m_wndList).

    if(hwnd)
    {
    CWindow win(hwnd);
    CHAR str[100];
    win.GetWindowText((LPTSTR)(str),100);
    if (strncmp(str,"searchstring", 13) == 0)
    dlg->m_wndList.push_back(hwnd);
    }

    It compiles, but I get the following error messages: Run-time Check Failure #2: Stack around the variable str is corrupted and/or Run-time Check Failure #2: Stack around the variable win is corrupted What is the problem? How should I change my code?

    C / C++ / MFC help question data-structures

  • Detours Simple sample applications dll export problem
    K keret

    Bingo! I copied it from an other project and I forgot about it. Man, it's so complicated!

    C / C++ / MFC c++ help com json announcement

  • Detours Simple sample applications dll export problem
    K keret

    Thanks, that worked. But I still don't know, why detoured.dll exported without "#define DETOURED_EXPORTS".

    C / C++ / MFC c++ help com json announcement

  • Detours Simple sample applications dll export problem
    K keret

    I'm new to C++. I try to build the Simple application in Microsoft's detours project in VS08. Detours is used to inject dlls into API processes or into applications. You can find it at: http://research.microsoft.com/en-us/projects/detours/ I managed to build: detoured.dll detours.lib Simple.dll (That's the hook) setdll.exe (The program that injects the hook) sleep5.exe (The exe that uses the hooked API) When I try to run the hook: setdll /d:simple.dll I get the error message: Error: simple.dll does not export function with ordinal #1. It's because Simple.dll doesn't export anything. Detoured.dll does export the function detoured. detoured.h:

    //////////////////////////////////////////////////////////////////////////////
    //
    // Presence of this DLL (detoured.dll) marks a process as detoured.
    //
    // Microsoft Research Detours Package, Version 2.1.
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //

    #ifdef DETOURED_EXPORTS
    #define DETOURED_API __declspec(dllexport)
    #else
    #define DETOURED_API __declspec(dllimport)
    #endif

    HMODULE DETOURED_API WINAPI Detoured();

    //
    ///////////////////////////////////////////////////////////////// End of File.

    detoured.cpp:

    //////////////////////////////////////////////////////////////////////////////
    //
    // Presence of this DLL (detoured.dll) marks a process as detoured.
    //
    // Microsoft Research Detours Package, Version 2.1.
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //

    #include #include "detoured.h"

    static HMODULE s_hDll;

    HMODULE WINAPI Detoured()
    {
    return s_hDll;
    }

    BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
    {
    (void)reserved;

    if (dwReason == DLL\_PROCESS\_ATTACH) {
        s\_hDll = hinst;
        DisableThreadLibraryCalls(hinst);
    }
    return TRUE;
    

    }

    //
    ///////////////////////////////////////////////////////////////// End of File.

    But simple.cpp is just this in the package:

    //////////////////////////////////////////////////////////////////////////////
    //
    // Detours Test Program (simple.cpp of simple.dll)
    //
    // Microsoft Research Detours Package, Version 2.1.
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //
    // This DLL will detour the Windows Sleep API so that TimedSleep function
    // gets called instead. TimedSleep records the before and after times, and
    // calls the real Sleep API through the TrueSleep function pointer.

    C / C++ / MFC c++ help com json announcement
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups