Network Printer Mapping C++/BaT Script

Posts 1–10 of 10 · Page 1 of 1
Network Printer Mapping C++/BaT Script
What's up guys! It's me again, I am going to borrow your brains again! Basically what I am trying to do is create a program or a script that will map a network (remote) Computer to a network printer.

Pseudo Structure:
printername = User Input
hostname = User Input

I want printer \\printerserver\printer to be mapped to computer \\computername.

'Assign' + printername + to computer' + hostname.

You know me I will link the whole source so you can have an idea of what I am trying to do.

Code:
#include <iostream>
#include <windows.h>
#include <string.h>

using namespace std;

static string hostname;
static string printername;

int main()
{
    cout << "This is Auto Mapper \n";
    cout << "Please Provide Target Hostname:";
    cin >> hostname;
    
    cout << "\nPlease Provide Printer Name:";
    cin >> printername;
    cout << "\nApplying Printer now..";
    system("rundll32.exe printerui.dll,printUIEntry /in /c \\" + hostname + "/n \\44vprn01\"" + printername"");
    
    return 0;
}
This is causing a compilation error pointing to an argument or how i'm initializing main(). Another thing which I will type out the output for you because I do not understand this..

expected ')' before string constant
cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >'to 'const char*' for argument '1' to 'int system(const char*)'

All of this code I did everything from head so I am pretty sure I am doing something wrong here. I did try to search for a function for mapping network printer and was unsuccessful. Any help would be most appreciated!
Code:
#include <iostream>
#include <windows.h>
#include <string.h>

using namespace std;

static char hostname[50];
static char printername[50];

int main()
{
    cout << "This is Auto Mapper \n";
    cout << "Please Provide Target Hostname:";
    cin >> hostname;
    
    cout << "\nPlease Provide Printer Name:";
    cin >> printername;
    cout << "\nApplying Printer now..";

    char final[255] = "\0";
    strcat(final, "rundll32.exe printerui.dll,printUIEntry /in /c \\");
    strcat(final, hostname);
    strcat(final, " /n \\44vprn01\"");
    strcat(final, printername);
    strcat(final, "\"");

    system(final);
    
    return 0;
}
There's probably a better way but I'm tired so w/e. You can't mix string with char* so yeahs (well not just simply like that anyway). Wouldn't coding a batch script be easier.

Code:
@EcHo off
echo This is Auto Mapper
echo Please Provide Target Hostname:
set /p hostname=

echo Please Provide Printer Name
set /p printername=

echo Applying Printer now...
set "str1=rundll32.exe printerui.dll,printUIEntry /in /c \\"
set "str2= /n \\44vprn01\"
set "str3=""

%str1%%hostname%%str2%%printername%%str3%
pause
Problem with this is.. It's like a big giant company i work for and they like to make sure everything is tight net (secured). So they complain if they see anything exposed or easily tweakable. I always create batch files, but i keep them in my usb drive and when I want to apply them to our network I need to convert them into c++ or try the method i'm doing now which is a blend of both.
Quote Originally Posted by xephora View Post
Problem with this is.. It's like a big giant company i work for and they like to make sure everything is tight net (secured). So they complain if they see anything exposed or easily tweakable. I always create batch files, but i keep them in my usb drive and when I want to apply them to our network I need to convert them into c++ or try the method i'm doing now which is a blend of both.
"Big Giant Company"
Use of the System(); Crap Function, Or executing Batch/Random Executable files from USB or everywhere ; No Security Policies at all.

No matter how many times I read these lines they are as funny.
Well saying The Board of Education which is a big giant company is something I would rather not meantion out loud. lol I get alot of heat when I say that considering everyone hates school. Well.. We manage all the schools so we still are hated by many. Oh and I do agree on "big giant company" sounding funny. Hell that's the only way I describe my company.
Code:
#include <iostream>
#include <windows.h>
#include <string.h>

using namespace std;

static string hostname;
static string printername;

int main()
{
    cout << "This is Auto Mapper \n";
    cout << "Please Provide Target Hostname:";
    cin >> hostname;
    
    cout << "\nPlease Provide Printer Name:";
    cin >> printername;
    cout << "\nApplying Printer now..";

    char sysstr[512] = "";
    sprintf_s(sysstr, 512, "rundll32.exe printerui.dll,printUIEntry /in /c \\%s/n \\44vprn01\"%s\"", hostname.c_str();, printername.c_str());
    system(sysstr);
    
    return 0;
}
assuming the end is meant to be like \44vprn01"lolwat"
Quote Originally Posted by Hell_Demon View Post
Code:
#include <iostream>
#include <windows.h>
#include <string.h>

using namespace std;

static string hostname;
static string printername;

int main()
{
    cout << "This is Auto Mapper \n";
    cout << "Please Provide Target Hostname:";
    cin >> hostname;
    
    cout << "\nPlease Provide Printer Name:";
    cin >> printername;
    cout << "\nApplying Printer now..";

    char sysstr[512] = "";
    sprintf_s(sysstr, 512, "rundll32.exe printerui.dll,printUIEntry /in /c \\%s/n \\44vprn01\"%s\"", hostname.c_str();, printername.c_str());
    system(sysstr);
    
    return 0;
}
assuming the end is meant to be like \44vprn01"lolwat"
It wouldn't accept that input. I even tried multiple slashes for network directory i forget that if you need to access \\printerserver\ you will need to type "\\\\printerserver\\", I still get an error though :\

in function int main()
invalid operands of types 'const char[48] and 'char[256]' to binary 'operator+'

Code:
char hostname[256];
char printername[256];

int main()
{
cout << "This is Auto Mapper \n";
cout << "Please Provide Target Hostname: \n ->";
cin >> hostname;

cout << "\nPlease Provide Printe Name: \n ->";
cin >> printername;
cout << "\nApplying Printer Now..";

system("RUNDLL32.exe PRINTUI.DLL,PrintUIEntry /in /c \\\\" + hostname + " /n \\\\44vprn01\\" + printername);

return 0;
}
This still failed as well, but I cleaned it up for you so it is a bit easier to understand really sorry about the mess.
Quote Originally Posted by xephora View Post
It wouldn't accept that input. I even tried multiple slashes for network directory i forget that if you need to access \\printerserver\ you will need to type "\\\\printerserver\\", I still get an error though :\

in function int main()
invalid operands of types 'const char[48] and 'char[256]' to binary 'operator+'

Code:
char hostname[256];
char printername[256];

int main()
{
cout << "This is Auto Mapper \n";
cout << "Please Provide Target Hostname: \n ->";
cin >> hostname;

cout << "\nPlease Provide Printe Name: \n ->";
cin >> printername;
cout << "\nApplying Printer Now..";

system("RUNDLL32.exe PRINTUI.DLL,PrintUIEntry /in /c \\\\" + hostname + " /n \\\\44vprn01\\" + printername);

return 0;
}
This still failed as well, but I cleaned it up for you so it is a bit easier to understand really sorry about the mess.

Your 2nd error was because you can't directly join two char arrays using a + operator, what was the actual error that using strings caused?
Code:
"rundll32.exe printerui.dll,printUIEntry /in /c \\" + hostname + "/n \\44vprn01\"" + printername""
Can't add char* (std::string operator) with const char*
[highlight=c++]#include <iostream>
#include <string>

#include <windows.h>

int main()
{
// You can use a _function_ scope using directive here. Do not pollute the global namespace.

std::string hostname;
std::string printername;

std::string process("rundll32.exe");
std::string cmdline("PRINTUI.dll,printUIEntry /in /c \\\\)";

std::cout << "Auto Mapper \n";
std::cout << "Input target hostname: ";
std::cin >> hostname;

std::cout << "\nInput printer name: ";
std::cin >> printername;

cmdline += hostname;
cmdline += " /n \\\\44vprn01\\";
cmdline += printername;

CreateProcess(process.c_str(), cmdline.c_str(), NULL, NULL, FALSE, NULL, NULL, NULL);

return 0;
}[/highlight]

Not sure if your command line parameters are correct. That is for you to check.
Posts 1–10 of 10 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Need help?