By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

P/Invoke using managed C++ error please help

P: n/a
I need call a LoginUser API from MC++ dll, but when I try to call the I have
always the same exception:

"System.NullReferenceException: Object reference not set to an instance of
an object.
at LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
Int32 dwLogonType, Int32 dwLogonProvider, IntPtr phToken)"

Thanks in advance

---------------------------------
this is the sample code

//MyClass.h

#pragma once
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::InteropServices;

namespace MyClassNameSpace
{
[DllImport( S"advapi32.dll",CharSet=CharSet::Auto, SetLastError=true)]
extern bool LogonUser( String* lpszUsername, String* lpszDomain, String*
lpszPassword,
int dwLogonType, int dwLogonProvider, IntPtr phToken);

public __gc class MyClass
{
// TODO: Add your methods for this class here.
public:
MyClass()
{
};
public:
void Login()
{
try
{
String *strUsername = new String( "username");
String *strDomain = new String( "domain");
String *strPassword = new String( "password");

IntPtr ptr = IntPtr::Zero;

bool bRet = LogonUser( strUsername, strDomain, strPassword, 3, 0, ptr);
}
catch (Exception *e)
{
System::Diagnostics::Debug::WriteLine( e);
}
};
};
}

//-------------TEST APPLICATION
//Form1.h

#pragma once
namespace test
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace MyClassNameSpace;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change
the
/// 'Resource File Name' property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly with
localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * button1;

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(108, 92);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 271);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->Load += new System::EventHandler(this, Form1_Load);
this->ResumeLayout(false);

}
private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
MyClass *myclass = new MyClass();
myclass->Login();
}

private: System::Void Form1_Load(System::Object * sender,
System::EventArgs * e)
{
}

};
}

// Form1.cpp

#include "stdafx.h"
#include "Form1.h"
#include <windows.h>

using namespace test;

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}

Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.