What is wrong with this program (that uses Windows Network functions)

This is a discussion on What is wrong with this program (that uses Windows Network functions) within the Windows Programming forums, part of the Platform Specific Boards category; I can't figure out how to get this program to work. It compiles fine. It doesn't work at school or ...

  1. #1
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209

    What is wrong with this program (that uses Windows Network functions)

    I can't figure out how to get this program to work. It compiles fine. It doesn't work at school or on my friends Lan at his house. The purpose of the program is to send anonymous net send messages but the functions don't seem to be doing anything and nothing happens when I run the program (stuff happens, but there is no result). I've read everything about the functions at the MSDN thing, but I don't know what I'm doing wrong. I've even tried manually registering the alias's with the system() function in replace of the NetMessageNameAdd() function. Here is the code (to compile it you have to link it with the netapi32 library):

    Code:
    #include <iostream>
    #include <cstring>
    #include <windows.h>
    #include <Lm.h>
    
    using namespace std;
    
    int main()
    {
        
        char fromName[20];
        char computerName[20];
        char message[100];
        char fake[30];
        char completeCommand[140];
        int num;
        
        cout<<"Welcome, you are about to have the best fun of"
            <<"\nyour typing career at the expense of someone else!"<<endl;
        
        cout<<"Please enter the name of the computer you want to spam: ";
        cin.getline(computerName, 20);
        char* compConst = computerName;
        
        cout<<"Please enter the message you wish to spam with (must be below 100 chars): ";
        cin.getline(message, 100);
        
        
        cout<<"Please enter the assumed name: ";
        cin.getline(fake, 30);
        char* nameConst = fake;
        
        cout<<"Please enter the number of messages you wish to send to that alias: ";
        cin>>num;
        
        int msg = strlen(message);
        NetMessageNameAdd(NULL, LPCWSTR("RECTOR"));
        for(int i = 0; i<num; i++){
        NetMessageBufferSend(NULL,(LPCWSTR)compConst, (LPCWSTR)nameConst, (BYTE*)message, msg*2);}
        
        cout<<"Done."<<endl;
        system("PAUSE");
        return 0;
    }
    Any help possible would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Well one thing to check, is to make sure the windows messenger service is running. I think it gets turned off by default when you install service pack 2. Does it work if you do the Net Send command manually?

  3. #3
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yes, the messenger service is running. At school and at my friend's house net send works manually.

    I also checked all of the function's return values and they are all coming out as error values.
    Last edited by homeyg; 02-16-2005 at 06:03 PM.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
        NetMessageBufferSend(NULL,(LPCWSTR)compConst, (LPCWSTR)nameConst, (BYTE*)message, msg*2);}
    Contrary to popular belief, you can't just cast a value to get rid of a warning and hope for the best. Occasionally, this approach works but often it doesn't.

    A LPCWSTR is typedefed to const wchar_t*. This means that it expects a wide unicode string.

    You have two choices to get wide strings for your function call. The first and preferred method is to use wide character input methods such as wcin. The second method is to continue to retrieve input in ansi and convert it to wide as needed for function calls. This second approach is demonstrated in this NetMessageBufferSend sample, the fourth result from Google.

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    What header file would I find wcin in?

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    wcin_3c__std


    - Stack Overlfow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    I can't get wcin to work. It keeps saying that wcin is undeclared.

  8. #8
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    The link I provided came with two examples of how to use wcin. Have you tried either of the examples, and does it still report wcin as undeclared? And if so, what is the exact compiler error.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  9. #9
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Yes, I've tried using both the examples on that site. Here is the error:

    23 C:\Documents and Settings\Owner\My Documents\John's Documents\Source Files C++\Spammer2000.cpp `wcin' undeclared (first use this function)
    I'm using Dev-C++.

    Also, in that google result posted by anonytmous, how does that program receive input? I don't know very much about all of these Windows functions and variable types. It almost looks as if the information need by NetMessageBufferSend is being passed in to the program as parameters. Is this true?
    Last edited by homeyg; 02-17-2005 at 02:05 PM.

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> I'm using Dev-C++. <<

    Try defining _GLIBCPP_USE_WCHAR_T before including <iostream>. Here is a sample that should work.
    Code:
    #define _GLIBCPP_USE_WCHAR_T 1
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(void)
    {
    	/* Get a line of input in a wstring. */
    	wstring input1;
    	getline(wcin, input1, L'\n');
    
    	/* Get a line of input in a c wide string. */
    	wchar_t input2[512];
    	wcin.getline(input2, 512);
    
    	wcout << input1 << endl << input2 << endl;
    
    	wcin.get();
    }
    >> It almost looks as if the information need by NetMessageBufferSend is being passed in to the program as parameters. Is this true? <<

    Yes. It uses command line arguments from the argv array. It would have been better to use CommandLineToArgvW to retrieve the command line arguments in unicode format, rather than converting the ansi version.

  11. #11
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What was wrong with my program?
    By cjmdjm in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 03:39 AM
  2. Use windows API functions in a GTK+ program?
    By Jake.c in forum Windows Programming
    Replies: 19
    Last Post: 01-23-2009, 06:40 AM
  3. Replies: 1
    Last Post: 01-24-2005, 02:07 PM
  4. Network Program
    By Necrodeemer in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2003, 03:16 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21