Menu

Calling a Pointer crashes my App.

stev
2008-10-02
2012-09-26
  • stev

    stev - 2008-10-02

    I have a function Init()
    My app crashes when it returns from the function.
    I have isolated the problem to the calling of a pointer. When it calls the pointer, it crashes. When it doesn't call the pointer, it doesn't crash.

    Here is the source code for Init() but the more interesting code is the code for the pointer. below

    [code]

    include "SerialShield_Sample.h"

    using namespace std;

    SerialShield_Sample::SerialShield_Sample()
    {

    };
    void SerialShield_Sample::SerialShield_Initialize()
    {
    // TODO: Add extra initialization here
    m_SSptr = new SerialShield;
    if (m_SSptr == NULL)
    {
    //MessageBox("Failed to initialize SerialShield class");
    MessageBox(NULL, "Failed to initialize SerialShield class", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);

    }
    int ret = m_SSptr->InitClass();
    if (ret == 2)
    {
         MessageBox(NULL, "Failed to initialize SerialShield.dll", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    
    }
    else if (ret == 1)
    {
    
        MessageBox(NULL, "Wrong version of SerialShield.dll", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    
    }
    
    Init();
    

    };

    void SerialShield_Sample::Init()
    {
    printf("Entered SerialShield_Sample::Init() \n\n");

    ApplicationName = "SerialShield Demo Sample";
    ApplicationKey  = "demo";
    
    // SerialShield Initialisation
    //I'm not sure how to use CStrin. I'm not sure I can access MFC with DEV C++ 
    //so I converted the values to string
    
    //SetApplicationInfo(LPTSTR, LPTSTR);
    m_SSptr->SetApplicationInfo("SerialShield Demo Sample", "demo");
    
    //m_SSptr->SetApplicationInfo(ApplicationName.GetBuffer(ApplicationName.GetLength()), ApplicationKey.GetBuffer(ApplicationKey.GetLength()));
    
    // Load a default key
    m_SSptr->SS_DefaultKey( "demo", "TL-CEA3205Bpb8K5yzQjJHEduwftE6WWHbom7pb5m3C" , "" );
    
    m_SSptr->SS_Initialize();
    
    // Detect if system clock has been moved back.
    if( m_SSptr->SS_TrialMode() == 99 )
    {    
        MessageBox(NULL, "Date moved Back", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    }    
    // Verification if trial period has been expired
    if( m_SSptr->SS_TrialExpired() )
    { 
        m_status = "Your Trial License has expired !";
        MessageBox(NULL, "Your Trial License has expired !", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    
        printf("Your Trial License has expired !");
    //  UpdateData(FALSE);
    }
    else
    {
        int t = m_SSptr->SS_TrialMode();
        switch (t)
        {
            // If SS_TrialMode = 1 Then Days Expiration
            case 1:
                m_status = m_SSptr->SS_LicenseInfo();
                m_status += " days left";
                //MessageBox(NULL, " days left", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    
            break;
            // If SS_TrialMode = 2 Then Date Expiration
            case 2:
                m_status = m_SSptr->SS_LicenseInfo();
                m_status += " date expires";
            //MessageBox(NULL, " date expires", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    
            break;
            // If SS_TrialMode = 3 Then Run Count Expiration
            case 3:
                m_status = m_SSptr->SS_LicenseInfo();
                m_status += " use(s) remain";
            //MessageBox(NULL, " use(s) remain", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    
            break;
            // If SS_TrialMode = 4 Then Free Mode
            case 4:
                m_status = m_SSptr->SS_LicenseInfo();
                m_status += " Trial Free Mode";
            //MessageBox(NULL, " Trial Free Mode", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    
            break;
        }
    }
    
    printf(m_status.c_str());
    
    printf("Problem #1:  I put system('PAUSE') to demonstrate that it crashes when this init function returns\n\n");
    
    // Check if the software is in unlocked mode
    if( m_SSptr->SS_IsUnlocked() )
    {
        m_status = "Registered Version";
    MessageBox(NULL,"Registered Version", "Information", MB_OK | MB_ICONINFORMATION | MB_SYSTEMMODAL);
    
    }
    printf("before printf(m_status.c_str()) \n");
    printf(m_status.c_str());
    printf("\nafter printf(m_status.c_str()) \n"); 
    m_editusername = m_SSptr->SS_GetUserName();
    m_editkey = m_SSptr->SS_GetUserKey();
    m_editmachineid = m_SSptr->GetHardwardID();
    m_editserialid = m_SSptr->SS_GetUserSerialID();
    //UpdateData(FALSE);
    

    system("PAUSE");

    return;
    };
    [/code]


    Below is the code for the pointer
    Here's the header
    [code]
    Since the calling of SerialShield* m_SSptr causes the crash on function exit , perhaps I should put the code for SerialShield here. It is attached but I'll put it here as well.

    SerialShield.h

    Code:

    // SerialShield.h: interface for the SerialShield class.
    //
    // This class is copyright Nic Wilson (C) 2005
    // nic@nicwilson.com
    //
    // It is provided as an example of using SerialShield only.
    // Distribution as example source code is permitted for IONWORX.COM
    // only. No other distribution is permitted without written consent
    // of the author.
    //
    // No changes to this file or files is permitted
    //
    // Distribution in compiled binary form is permitted at no cost whatsoever,
    // however acknowlegment of use and copyright must be shown in the "about"
    // window of the application using this code, and in the documentation.
    // such as: "Some code portions Copyright Nic Wilson nic@nicwilson.com"
    //
    //
    // Version History
    // V1.00 16th July 2005 - Initial Release
    //////////////////////////////////////////////////////////////////////

    if !defined(AFX_SERIALSHIELD_H5C4DA086_C149_472E_AF8B_DAC673603DA2INCLUDED_)

    define AFX_SERIALSHIELD_H5C4DA086_C149_472E_AF8B_DAC673603DA2INCLUDED_

    if _MSC_VER > 1000

    pragma once

    endif // _MSC_VER > 1000

    include <windows.h>

    class SerialShield
    {
    public:
    //All these are public as they need to be accessed from outside the class.
    SerialShield();
    virtual ~SerialShield();
    char (GetHardwardID) (void);
    int (SSUser) (char Fullname, char Key, char SerialID);
    bool (SS_TrialExpired) (void);
    bool (
    SS_RemoveKey) (void);
    bool (SS_IsUnlocked) (void);
    char
    (SS_LicenseInfo) (void);
    char
    (SS_GetUserName) (void);
    char
    (SS_GetUserKey) (void);
    char
    (SS_GetUserSerialID)(void);
    int (
    SS_R) (char Name, char Key);
    int (SS_TrialMode) (void);
    char
    (TripleDESEncrypt) (char Text, char Key);
    char
    (TripleDESDecrypt) (char Text, char Key);
    void (
    SS_Initialize) (void);
    void (SetApplicationInfo)(char ApplicationName, char SoftKey);
    void (
    Antidebugging) (void);
    void (AntiMonitors) (void);
    void (
    SS_DefaultKey) (char Username, char LicenseKey, char* SerialID);
    BOOL InitClass();

    private:
    //This is for class member use only.
    HINSTANCE m_hSS;
    };

    endif // !defined(AFX_SERIALSHIELD_H5C4DA086_C149_472E_AF8B_DAC673603DA2INCLUDED_)

    [/code]

    Here's the cpp for the pointer
    [code]

    include "SerialShield.h"

    include <windows.h>

    include <iostream>

    ifdef _DEBUG

    undef THIS_FILE

    static char THIS_FILE[]=FILE;

    define new DEBUG_NEW

    endif

    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////

    SerialShield::SerialShield()
    {
    printf("\nentered SerialShield() \n");

    }

    SerialShield::~SerialShield()
    {
    printf("\nentered ~SerialShield() \n");

    if (m_hSS)
        FreeLibrary(m_hSS);
    
        printf(&quot;\nexited ~SerialShield() \n&quot;);
        system(&quot;PAUSE&quot;);
    

    }

    BOOL SerialShield::InitClass()
    {
    m_hSS = LoadLibrary("SerialShield.dll");
    if (m_hSS == NULL)
    return 2;
    GetHardwardID = (char()(void)) GetProcAddress( m_hSS, "GetHardwardID");
    SSUser = (int ()(char, char, char))GetProcAddress( m_hSS, "SSUser");
    SS_TrialExpired = (bool ()(void)) GetProcAddress( m_hSS, "SS_TrialExpired");
    SS_RemoveKey = (bool (
    )(void)) GetProcAddress( m_hSS, "SS_RemoveKey");
    SS_IsUnlocked = (bool ()(void)) GetProcAddress( m_hSS, "SS_IsUnlocked");
    SS_LicenseInfo = (char
    ()(void)) GetProcAddress( m_hSS, "SS_LicenseInfo");
    SS_GetUserName = (char
    ()(void)) GetProcAddress( m_hSS, "SS_GetUserName");
    SS_GetUserKey = (char
    ()(void)) GetProcAddress( m_hSS, "SS_GetUserKey");
    SS_GetUserSerialID = (char
    ()(void)) GetProcAddress( m_hSS, "SS_GetUserSerialID");
    SS_R = (int (
    )(char, char)) GetProcAddress( m_hSS, "SS_R");
    SS_TrialMode = (int ()(void)) GetProcAddress( m_hSS, "SS_TrialMode");
    TripleDESEncrypt = (char
    ()(char, char)) GetProcAddress( m_hSS, "TripleDESEncrypt");
    TripleDESDecrypt = (char
    ()(char, char)) GetProcAddress( m_hSS, "TripleDESDecrypt");
    SS_Initialize = (void (
    )(void)) GetProcAddress( m_hSS, "SS_Initialize");
    SetApplicationInfo = (void ()(char, char)) GetProcAddress( m_hSS, "SetApplicationInfo");
    Antidebugging = (void (
    )(void)) GetProcAddress( m_hSS, "Antidebugging");
    AntiMonitors = (void ()(void)) GetProcAddress( m_hSS, "AntiMonitors");
    SS_DefaultKey = (void (
    )(char, char, char*))GetProcAddress( m_hSS, "SS_DefaultKey");

    bool fail = false;
    if (GetHardwardID == NULL)
        fail = true;
    if (SSUser == NULL)
        fail = true;
    if (SS_TrialExpired == NULL)
        fail = true;
    if (SS_RemoveKey == NULL)
        fail = true;
    if (SS_IsUnlocked == NULL)
        fail = true;
    if (SS_LicenseInfo == NULL)
        fail = true;
    if (SS_GetUserName == NULL)
        fail = true;
    if (SS_GetUserKey == NULL)
        fail = true;
    if (SS_GetUserSerialID == NULL)
        fail = true;
    if (SS_R == NULL)
        fail = true;
    if (SS_TrialMode == NULL)
        fail = true;
    if (TripleDESEncrypt == NULL)
        fail = true;
    if (TripleDESDecrypt == NULL)
        fail = true;
    if (SS_Initialize == NULL)
        fail = true;
    if (SetApplicationInfo == NULL)
        fail = true;
    if (Antidebugging == NULL)
        fail = true;
    if (AntiMonitors == NULL)
        fail = true;
    if (SS_DefaultKey == NULL)
        fail = true;
    if (fail)
    {
        FreeLibrary(m_hSS);
        m_hSS = NULL;
        return 1;
    }
    return 0;
    

    }

    [/code]

    It would be a dream come true if I could figure this thing out.
    Is there something inherent in this cod that would allow it to work in Visual C++ with MFC and not on Dev C++ 4992?

    When I say crash, i mean that it never returns to main code.
    The function Init() does return to the calling function ::SerialShield_Initialize();
    But it crashes before returning to the main and it doesn't call any destructores.
    Crash is indicated by a message from the Vista operating system which says

    SutherlandSample2Console.exe has stopped working.

    A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.

    Thank very much.

    Stephen
    PS If anyone wants to see the whole code including the dev project file, please let me know and I'll email it to you.

     

    Related

    Code: code

    • cpns

      cpns - 2008-10-02

      You have posted a large amount of code that is nonetheless incomplete. You have also not highlighted where this "calling of a pointer" occurs. I assume that you mean calling a function through a pointer? You are making us work too hard!

      Init() calls several function through pointers - any particular one!?

      Is there are reason for using LoadLibrary/GetProcAddress rather than linking an import library?

      Have you tried using the debugger? It is a more robust method of finding the point of failure. Changing the code may be changing the behaviour in ways that lead you to false conclusions.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.