Home Index

System CGI Prog. in Windows NT/2000, Change Password CGI prograam
Date: June  01 2001
Subject: Online Programmer Ezine, Volume 2, No.5
Article: System CGI Prog. in Windows NT/2000, Change Password CGI prograam

In previous issues of this ezine we have seen sample codes 
for CGI programming in C. We saw how we can do this from
base with our own code.

We also saw how we can write simple cgi applications.
If you have not received this articles you can see
them on our site.

In this issue we want to see how we can write system
administration CGI programs for windows NT/2000 
platforms. To be able to focus on system programming
issues we will use a ready CGI Library.

You can download a copy of "CGI-LIB Library by
Noel V Aguilar" from internet. After download you must
compile library source codes.

We have used a Win32 Console Application project in
Visual C++ environment. We have also added two library 
files to our project. First one is NETAPI32.LIB that
comes with Visual C++ . Second one is CGILIB.LIB that
you have obtained from compiling the source code of 
CGI-LIB . If you want you can obtain all of the project
files from our web site.

In this article we will use "NetUserChangePassword" 
function from NetAPI to change user passwords from a 
remote browser.

We have used a few of the most useful functions of

cgi_input_parse() function receives data that is sent
from browser with post and get method and parses it.

After this we will be able to get the value of a
variable sent by browser with find_val() function.
This function will use a pointer to parsed string that
we obtained in previous step.

mime_header() function will prepare needed headers for
a html/text type output for web server. This will 
determine type of data that will be sent to web server.

html_begin() function will add <html>, <head> and <title>
tags to web page that we are trying to create.
It accepts a title parameter and will use it between 
<title>...</title> tags in our web page.

We have also used "MultiByteToWideChar" function to convert
ordinary Ansi strings to WideString that is needed for
"MultiByteToWideChar" function parameters.

After calling "MultiByteToWideChar" function we will 
see return values to handle possible errors.

We have blocked changing Administrator password over the
web for security reasons.

And at the end we have used html_end() CGI-LIB function
to add </BODY> and </HTML> tags to the end of web page.

And finally we have done it.

In bellow source code we have forced to break long lines 
into two or more lines.

#ifndef UNICODE
#define UNICODE

#include <stdio.h>
#include <stdlib.h>
#include <windows.h> 
#include <lm.h>
#include <tchar.h>
#include "cgi-lib.h"
#include "html-lib.h"

void handle_error(int n);

int main()

DWORD dwError = 0;
int ln,c;

wchar_t name[20],newpassword[20],oldpassword[20];

char npassword[20],opassword[20],id[20];

LIST *head;

head = cgi_input_parse();

html_begin("Change Password Confirmation",NULL);
if(head == NULL)






wprintf(L"<br>Changing Password for \"%s\"<br>",name);

nStatus = 

if (nStatus == NERR_Success)
printf("<br><br><b>Password for user \"%s\" has been 
successfully changed.</b>",id );
else if (nStatus == 2221)
printf("<br><b>User not found</b>");
else if (nStatus == 86)
printf("<br><b>Old Password Not Correct</b>");
printf("\n<br><br><b><font color=\"#FF0000\">Error 
changing password : %4d</FONT></b>", nStatus);
printf("<br><b>Password for administrator can not be 


return 0;


void handle_error(int n)
printf("Error in Cgi input parameters");


If you have any question you can ask your questions 
in our support forums at:


Home Index
2001 OnlineProgrammer and (Htmsoft.com) . All Rights Reserved