NetUnjoinDomain causes a memory error

gareth evans gareth.evans at wideawake.co.uk
Wed Jul 17 06:31:34 EDT 2002


I've written a c extension for python to use some netapi32 functions
i found that I get a memory error when calling NetUnjoin domain
test.py=

#!/bin/python
import netapi
import time

NETSETUP_JOIN_DOMAIN=0x00000001
NETSETUP_ACCT_CREATE=0x00000002
NETSETUP_ACCT_DELETE=0x00000004
NETSETUP_WIN9X_UPGRADE=0x00000010
NETSETUP_DOMAIN_JOIN_IF_JOINED=0x00000020
NETSETUP_JOIN_UNSECURE=0x00000040
NETSETUP_INSTALL_INVOCATION=0x00040000

UNAME="Administrator"
PWORD="w0rf"

def test_func():
	
	print "in test script"
	print "in net unjoin domain"
	status=netapi.NetUnjoinDomain(None,UNAME,PWORD,NETSETUP_ACCT_DELETE)
	print status
	return

test_func()


$ ./test.py
in test script
in net unjoin domain

<then i get an error box>

The instruction at "0x40100000" referenced memory at "0x40100000". The
memory could not be "read".

this seems to be happening during the garbage collection after the
function exits ive no idea why it also only seems to occur if you make
any refrence to the py_lpPassword after the PyArg_ParseTuple if i use
a diffrent variable for the password e.g. the comented py_lpPassword1
it works fine. I'm really stuck.
any ideas? 

//-  NetGetDCName - netapi32
//NetGetJoinInformation - netapi32
//NetJoinDomain - netapi32
//NetUnjoinDomain - netapi32
//SetComputerName - kernel32
#pragma comment(lib, "NETAPI32.LIB")
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <iostream.h>
#include "Python.h"
#include "TConvert.h"
#include "netapi_py.h"
#include "LMJOIN.h"

static PyObject *NetapiError;


PyObject *Net_GetJoinInformation(PyObject *self, PyObject *args)
{	
	LPWSTR lpServer;
	LPWSTR lpNameBuffer;
	CHAR* py_NameBuffer;
	LPSTR py_Server;
	PNETSETUP_JOIN_STATUS BufferType;
	int status;
	BufferType=new NETSETUP_JOIN_STATUS;
	if (!PyArg_ParseTuple(args, "z",&py_Server))
		return NULL;
	if (py_Server!=NULL)
	{
		_towchar c1(py_Server);
		lpServer=c1;
		status=NetGetJoinInformation(lpServer, &lpNameBuffer, BufferType);	
	}
	else status=NetGetJoinInformation(NULL, &lpNameBuffer, BufferType);
	if (status==0)
	{
		_tochar c2(lpNameBuffer);
		py_NameBuffer=c2;
		delete lpServer;
		return Py_BuildValue("isi",0,py_NameBuffer,*BufferType);
	}
	else{
		return Py_BuildValue("isi",1,"NetGetJoinInformation Failed",status);
	}
}

PyObject *Net_JoinDomain(PyObject *self, PyObject *args)
{
	NET_API_STATUS status;
	LPCWSTR lpServer;
	LPCWSTR lpDomain;
	LPCWSTR lpAccountOU;
	LPCWSTR lpAccount;
	LPCWSTR lpPassword;
	CHAR* py_lpServer;
	CHAR* py_lpDomain;
	CHAR* py_lpAccountOU;
	CHAR* py_lpAccount;
	CHAR* py_lpPassword;
	DWORD fJoinOptions;
	//printf("%",NETSETUP_JOIN_DOMAIN);
	if (!PyArg_ParseTuple(args, "zszzzd",&py_lpServer, &py_lpDomain,
&py_lpAccountOU,
		&py_lpAccount, &py_lpPassword, &fJoinOptions))
			return NULL;
	if (py_lpServer!=NULL)
	{
		_towchar c1(py_lpServer);
		lpServer=c1;
	}
	else lpServer=NULL;
	_towchar c2(py_lpDomain);
	lpDomain=c2;

	if (py_lpAccountOU!=NULL)
	{
		_towchar c3(py_lpAccountOU);
		lpAccountOU=c3;
	}
	else lpAccountOU=NULL;

	if (py_lpAccount!=NULL)
	{
		_towchar c4(py_lpAccount);
		lpAccount=c4;
	}
	else lpAccount=NULL;

	if (py_lpPassword!=NULL)
	{
		_towchar c5(py_lpPassword);
		lpPassword=c5;
	}
	else lpPassword=NULL;

	status=NetJoinDomain(lpServer, lpDomain, lpAccountOU, lpAccount,
lpPassword, fJoinOptions);
	return Py_BuildValue("i",status);
}

PyObject *Net_UnjoinDomain(PyObject *self, PyObject *args)
{
	NET_API_STATUS status;
	LPCWSTR lpServer;
	LPCWSTR lpAccount;
	LPCWSTR lpPassword;
	DWORD   fUjoinOptions;
	CHAR* py_lpServer;
	CHAR* py_lpAccount;
	//CHAR* py_lpPassword1="Tiger";
	CHAR* py_lpPassword;
	if (!PyArg_ParseTuple(args, "zzzd",&py_lpServer, &py_lpAccount,
&py_lpPassword, &fUjoinOptions))
		return NULL;
	if (py_lpServer!=NULL)
	{
		_towchar c1(py_lpServer);
		lpServer=c1;
	}
	else lpServer=NULL;
	
	if (py_lpAccount!=NULL)
	{
		
		_towchar c2(py_lpAccount);
		lpAccount=c2;
	}
	else lpAccount=NULL;
	//printf("%s\n",py_lpPassword);
	if(py_lpPassword!=NULL)
	{
		_towchar c9((LPCSTR)py_lpPassword);
		lpPassword=(LPCWSTR)c9;
		printf("\n");
	}
	//lpPassword=NULL;
	else lpPassword=NULL;
	//delete py_lpPassword;
	status=NetUnjoinDomain(lpServer, lpAccount, lpPassword,
fUjoinOptions);
	//printf ("here");
	return Py_BuildValue("i",status);
}

PyObject *Net_GetDCName(PyObject *self, PyObject *args)
{
	NET_API_STATUS status;
	LPCWSTR servername;  
	LPCWSTR domainname;
//	LPCWSTR result;
	LPBYTE *bufptr;      
	CHAR* py_servername;
	CHAR* py_domainname;
//	CHAR* py_result;
	int len();
	if (!PyArg_ParseTuple(args, "zz",&py_servername, &py_domainname))
		return NULL;
		
	if (py_servername!=NULL)
	{
		_towchar c1(py_servername);
		servername=c1;
	}

	else servername=NULL;

	if (py_domainname!=NULL)
	{
		_towchar c2(py_domainname);
		domainname=c2;
	}
	else domainname=NULL;

	status=NetGetDCName(servername, domainname, bufptr);
	return Py_BuildValue("is",status,bufptr);
}

PyObject *Set_ComputerName(PyObject *self, PyObject *args)
{
	BOOL status;
	LPCTSTR lpComputerName;
	CHAR* py_lpComputerName;
	DWORD err_code;

	if (!PyArg_ParseTuple(args, "s",&py_lpComputerName))
		return NULL;

	_towchar c1(py_lpComputerName);
	lpComputerName=c1;

	status=SetComputerName(lpComputerName );
	if (status!=0)//worked
	{
		return Py_BuildValue("is",0,"WORKED");
	}
	else
	{
	err_code=GetLastError();
	return Py_BuildValue("ds",err_code,"FAILED");
	}
}

static PyMethodDef NetMethods[] = {
    {"NetGetJoinInformation",  Net_GetJoinInformation, METH_VARARGS},
	{"NetJoinDomain", Net_JoinDomain, METH_VARARGS},
	{"NetUnjoinDomain", Net_UnjoinDomain, METH_VARARGS},
	{"NetGetDCName",Net_GetDCName,METH_VARARGS},
	{"SetComputerName",Set_ComputerName,METH_VARARGS},
    {NULL, NULL}      
};

#ifdef MS_WIN32
__declspec(dllexport)
#endif

DL_EXPORT(void)
initnetapi(void){
	PyObject *m, *d;

	m=Py_InitModule("netapi",NetMethods);
	d = PyModule_GetDict(m);
	NetapiError = PyErr_NewException("netapi.error",NULL,NULL);
	PyDict_SetItemString(d,"error",NetapiError);
}



More information about the Python-list mailing list