Reading a string in VC++

R

Rajeev

Hi
I am a VB programmer. I need to do a simple thing in VC++.
char strNWUserName[256];
DWORD dBufferLength = 256;
HRESULT hr;
hr = WNetGetUserA("DOMAIN", strNWUserName, &dBufferLength);
The strNWUserName returns .CN=UserName.O=DOMAIN

I just need to extract the UserName from this using VC++. I don't need
the .CN= and O.=DOMAIN.

I am typing the code in VB to do this.

Public Function NWUserName() As String
Dim strNWUserName As String
Dim lngBufferLen As Long
Dim lngResult As Long
Dim lngSecondDotPos As Long

Const NO_ERROR = 0
Const ERROR_NOT_CONNECTED = 2250&
Const ERROR_MORE_DATA = 234&
Const ERROR_NO_NETWORK = 1222&
Const ERROR_EXTENDED_ERROR = 1208&
Const ERROR_NO_NET_OR_BAD_PATH = 1203&

Const cNWResource = "DOMAIN"
'name of a network resource this user
'is connected to (name of the NDS tree, for example)

strNWUserName = String$(255, vbNullChar)
lngBufferLen = Len(strNWUserName)
lngResult = apiWNetGetUser(cNWResource, strNWUserName, lngBufferLen)

If lngResult = NO_ERROR Then
'on Netware the API returns full distinguished name,
'parse it here to get the login name
'eg: .CN=Login.O=Container -Login
If strNWUserName Like ".CN=*.O=*" Then
lngSecondDotPos = InStr(1, strNWUserName, ".O=", vbTextCompare)
NWUserName = Mid$(strNWUserName, 5, lngSecondDotPos - 5)
Else
Err.Raise vbObjectError Or 1001, "NWUserName", _
"Not a Netware login name."
End If
Else
Select Case lngResult
Case ERROR_NOT_CONNECTED
Err.Raise vbObjectError Or 1002, "NWUserName", "'" & _
cNWResource & "' is not a connected network resource."
Case ERROR_MORE_DATA
Err.Raise vbObjectError Or 1003, "NWUserName", _
"More entries are available with subsequent calls."
Case ERROR_NO_NETWORK
Err.Raise vbObjectError Or 1004, "NWUserName", _
"The network is unavailable."
Case ERROR_EXTENDED_ERROR
Err.Raise vbObjectError Or 1005, "NWUserName", _
"A network-specific error has occured."
Case ERROR_NO_NET_OR_BAD_PATH
Err.Raise vbObjectError Or 1006, "NWUserName", _
"No network provider accepted the given network path '" & _
cNWResource & "'."
Case Else
Err.Raise vbObjectError Or 1007, "NWUserName", _
"An unknown error has occured."
End Select
End If

End Function
 
J

John Harrison

Rajeev said:
Hi
I am a VB programmer. I need to do a simple thing in VC++.
char strNWUserName[256];
DWORD dBufferLength = 256;
HRESULT hr;
hr = WNetGetUserA("DOMAIN", strNWUserName, &dBufferLength);
The strNWUserName returns .CN=UserName.O=DOMAIN

I just need to extract the UserName from this using VC++. I don't need
the .CN= and O.=DOMAIN.

Simply enough, you just have to learn C++ string handling instead of VB
string handling.

How about this (untested), I'm assuming that strNWUserName is a null
terminated C string, which I immediately convert to a C++ string.

#include <string>
using namespace std;

char strNWUserName[256];
DWORD dBufferLength = 256;
hr = WNetGetUserA("DOMAIN", strNWUserName, &dBufferLength);
string user_name(strNWUserName);
if (user_name.compare(0, 4, ".CN=") != 0)
{
// not a NW username
return;
}
string::size_type pos = user_name.find(".O=", 4);
if (pos == string::npos)
{
// not a NW username
return;
}
string nw_user_name = user_name.substr(4, pos - 4);

john
 
C

Chris \( Val \)

| Hi
| I am a VB programmer. I need to do a simple thing in VC++.
| char strNWUserName[256];
| DWORD dBufferLength = 256;
| HRESULT hr;
| hr = WNetGetUserA("DOMAIN", strNWUserName, &dBufferLength);
| The strNWUserName returns .CN=UserName.O=DOMAIN
|
| I just need to extract the UserName from this using VC++. I don't need
| the .CN= and O.=DOMAIN.
|
| I am typing the code in VB to do this.

:).

# include <iostream>
# include <ostream>
# include <string>

inline std::string ExtractUsername( const std::string& Source )
{
static const std::string Token( "CN=" );
std::string::size_type Start( Source.find( Token ) );

std::string::size_type End(
Source.find_first_of( '.', Start + Token.size() ) );

if( Start != std::string::npos && End != std::string::npos )
return Source.substr( Start + Token.size(), End - Start - Token.size() );

return std::string();
}

int main()
{
std::string UserName = ExtractUsername( ".CN=UserName.O=DOMAIN" );
std::cout << UserName << std::endl;

return 0;
}

Cheers.
Chris Val
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top