Thread: simple connection program

  1. #1
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69

    Thumbs down simple connection program

    I can't get this program to compile. I have a feeling I am doing something really stupid,
    but I can not remember how to fix it.

    main.cpp
    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include "resource.h"
    #include <winnetwk.h>
    #include <stdio.h>
    HINSTANCE hInst;
    LPNETRESOURCE network;
    network->dwScope=RESOURCE_CONNECTED;
    network->dwType=RESOURCETYPE_ANY;
    network->dwDisplayType=RESOURCEDISPLAYTYPE_GENERIC;
    network->dwUsage=RESOURCEUSAGE_CONNECTABLE;
    network->lpLocalName=NULL;
    network->lpRemoteName="\\\\192.168.0.4\\share";
    network->lpComment="A connection for testing...";
    BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch(uMsg)
        {
        case WM_INITDIALOG:
            return TRUE;
    
        case WM_CLOSE:
            EndDialog(hwndDlg, 0);
            return TRUE;
    
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
            case IDC_BTN_QUIT:
                EndDialog(hwndDlg, 0);
                return TRUE;
    
            case IDC_BTN_DISABLE:
                //SetCursorPos(0,0);
                //mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); /* Left button down. */
                //mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); /* Left button up. */
                //Disable the EDITTEXT control
                return TRUE;
            }
        }
    
        return FALSE;
    }
    
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
        hInst = hInstance;
        LPTSTR netprovnm;
        unsigned long x;
        char error_message[100];
        DWORD data;
        DWORD *bufsize;
        *bufsize=100L;
        data=WNetGetProviderName(0x10010000, netprovnm, bufsize);
        if(data==ERROR_MORE_DATA)
        {
            MessageBox(HWND_DESKTOP, "Buffer size is too small for network\nprovider name. Trying again.", "Error", MB_OK | MB_ICONWARNING);
        }
        else if(data==ERROR_NO_NETWORK)
        {
            MessageBox(HWND_DESKTOP, "Network is not available.", "Error", MB_OK | MB_ICONERROR);
            return 0;
        }
        else if(data==ERROR_INVALID_ADDRESS)
        {
            MessageBox(HWND_DESKTOP, "Invalid address.", "Error", MB_OK | MB_ICONERROR);
            return 0;
        }
        else if(data==NO_ERROR)
        {
            network->lpProvider=netprovnm;
        }
        while(1) {
            data=WNetAddConnection2(network, "", NULL, CONNECT_UPDATE_PROFILE);
            if(data==NO_ERROR)
            {
                MessageBox(HWND_DESKTOP, "Successfully connected to network!", "Info", MB_OK);
            }
            else
            {
                x=data;
                sprintf(error_message, "Error code: %d", x);
                if(MessageBox(HWND_DESKTOP, error_message, "Error", MB_ABORTRETRYIGNORE | MB_ICONERROR | MB_DEFBUTTON2)!=IDRETRY)
                {
                    break;
                }
            }
        }
        // The user interface is a modal dialog box
        return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
    }
    resource.h & resource.rc contain nothing of importance.
    What this program is supposed to do is create a connection, announce that the connection worked, and then do something else. This is the first step of a program I plan to write.
    Even a little help would be appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Benji Wiebe View Post
    I can't get this program to compile. I have a feeling I am doing something really stupid,
    but I can not remember how to fix it.
    main.cpp
    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include "resource.h"
    #include <winnetwk.h>
    #include <stdio.h>
    HINSTANCE hInst;
    LPNETRESOURCE network;
    
    
    network->dwScope=RESOURCE_CONNECTED;         <--- this should be inside WinMain
    network->dwType=RESOURCETYPE_ANY;
    network->dwDisplayType=RESOURCEDISPLAYTYPE_GENERIC;
    network->dwUsage=RESOURCEUSAGE_CONNECTABLE;
    network->lpLocalName=NULL;
    network->lpRemoteName="\\\\192.168.0.4\\share";
    network->lpComment="A connection for testing...";

  3. #3
    A source of questions... Benji Wiebe's Avatar
    Join Date
    Mar 2011
    Location
    Durham, Kansas
    Posts
    69

    locked up...

    Thanks. That got rid of the errors, but now when I run the program,
    it locked the computer up. Another stupid mistake?!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. A tenet connection from a C program..
    By ahmd2080 in forum Linux Programming
    Replies: 2
    Last Post: 07-04-2009, 03:42 AM
  3. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  4. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  5. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM