473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

P/Invoke using managed C++ error please help

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
0 1857

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: pawel | last post by:
Hi! I have a .dll library, with some exported function, which looks something like this: extern "C" { int exported_f (int (__cdecl *callback) (int a, int b)) ; } now, I want to invoke this...
11
by: Yoni Rabinovitch | last post by:
Is it possible to invoke a C# delegate/event handler asynchronously, from unmanaged C++ ? I assume this requires a Managed C++ wrapper, which would call BeginInvoke on the delegate ? Is this...
2
by: Vinay | last post by:
Hi , I am having server and client. Client is a test machine. I want to invoke the application binary which resides on server from client machine. The binary is created using C#. Please note that...
8
by: Don | last post by:
I have a third party C++ DLL that I am trying to use from C#. The specific function I am trying to use is declared in C++ as follows: ladybugConvertToMultipleBGRU32( LadybugContext ...
16
by: Duncan Mole | last post by:
Hi, This is probably an easy one but it iy first bit of p/invoke. I am trying to use the following C struct in a call: typedef struct { BYTE SRB_Cmd; BYTE SRB_Status, BYTE ...
5
by: RickDee | last post by:
Please help, anybody. I am trying to write a program so that it can launch an exe file ( which is also genereated in C# ) and then simulate the button clicking and invoke the methods inside the...
7
by: stephan querengaesser | last post by:
hi ng, i try to invoke a webservice-method with an filter-object, that contains value types. if i donīt want to filter the return value of the method, i have to pass a new instance of the...
5
by: Chris | last post by:
We are using SQLite.net in an application but getting a bottleneck at the data access layer (it has been profiled). This is clearly because p/invoke takes 5 to 10 times longer than it does calling...
4
by: andrewcw | last post by:
Can someone suggest a way to get the functions to match - I am trying to get file versions, etc info. But its still in unmanaged code as far as I can find. VGetFileVersionInfoSizeA' has unbalanced...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.