Call NetMessageBufferSend from a c# service return 2273 error

L

Loïc Delambre

Hi,

I have a service made in csharp language and I try to send message to
network computer with the NetMessageBufferSend.
Here's the import of the function :
[DllImport("netapi32.dll",CharSet=CharSet.Unicode,ExactSpelling=false,
SetLastError=true)]
static extern int NetMessageBufferSend(
/*[MarshalAs(UnmanagedType.LPWStr)] */string servername,
/*[MarshalAs(UnmanagedType.LPWStr)] */string msgname,
/*[MarshalAs(UnmanagedType.LPWStr)] */string fromname,
/*[MarshalAs(UnmanagedType.LPWStr)] */string buf,
int buflen);

The call failed with the 2273 return code "Alias not found on the network",
but if I use the net send in a command window, I can send the message. I
tried to change the service account with "local system" or a "domain admin",
but I failed too.

It seems that it is a right problem, but I can't find it.
 
W

Wiktor Zychla

I have a service made in csharp language and I try to send message to
network computer with the NetMessageBufferSend.

this works for me:

public static int NetSend( string serverName, string msgName, string
fromName, string message )
{
try
{
byte[] buf = System.Text.Encoding.Unicode.GetBytes( message );
return NetMessageBufferSend( serverName, msgName, fromName, buf,
buf.Length );
}
catch ( Exception ex )
{
...
}
}


[DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
static extern int NetMessageBufferSend(
string serverName,
string msgName,
string fromName,
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.U1,
SizeParamIndex=4)]
byte[] buf,
[MarshalAs(UnmanagedType.U4)]
int bufLen);

Wiktor Zychla
 
L

Loïc Delambre

Wiktor Zychla said:
I have a service made in csharp language and I try to send message to
network computer with the NetMessageBufferSend.

this works for me:

public static int NetSend( string serverName, string msgName, string
fromName, string message )
{
try
{
byte[] buf = System.Text.Encoding.Unicode.GetBytes( message );
return NetMessageBufferSend( serverName, msgName, fromName, buf,
buf.Length );
}
catch ( Exception ex )
{
...
}
}


[DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
static extern int NetMessageBufferSend(
string serverName,
string msgName,
string fromName,
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.U1,
SizeParamIndex=4)]
byte[] buf,
[MarshalAs(UnmanagedType.U4)]
int bufLen);

Wiktor Zychla

Thanks, I will test this declaration.
 
W

Willy Denoyette [MVP]

Only "Administrators" and "Server Operators" can call this function when
running on member servers and workstations.
Or simply put - you shouldn't call this from a service.
Willy.
 
L

Loïc Delambre

Loïc Delambre said:
Wiktor Zychla said:
I have a service made in csharp language and I try to send message to
network computer with the NetMessageBufferSend.

this works for me:

public static int NetSend( string serverName, string msgName, string
fromName, string message )
{
try
{
byte[] buf = System.Text.Encoding.Unicode.GetBytes( message );
return NetMessageBufferSend( serverName, msgName, fromName, buf,
buf.Length );
}
catch ( Exception ex )
{
...
}
}


[DllImport("netapi32.dll", CharSet=CharSet.Unicode)]
static extern int NetMessageBufferSend(
string serverName,
string msgName,
string fromName,
[MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.U1,
SizeParamIndex=4)]
byte[] buf,
[MarshalAs(UnmanagedType.U4)]
int bufLen);

Wiktor Zychla

Thanks, I will test this declaration.
It don't work for me in the service. I changed my code to use the net.exe
command in a thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top