C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading a string in VC++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Rajeev
Guest





PostPosted: Fri Mar 05, 2004 1:19 am    Post subject: Reading a string in VC++ Reply with quote



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
Back to top
John Harrison
Guest





PostPosted: Fri Mar 05, 2004 6:37 am    Post subject: Re: Reading a string in VC++ Reply with quote




"Rajeev" <navvyus (AT) yahoo (DOT) com> wrote

Quote:
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



Back to top
Chris ( Val )
Guest





PostPosted: Fri Mar 05, 2004 11:44 am    Post subject: Re: Reading a string in VC++ Reply with quote




"Rajeev" <navvyus (AT) yahoo (DOT) com> wrote

Quote:
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.

Smile.

# 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



Back to top
Rajeev
Guest





PostPosted: Sat Mar 06, 2004 12:33 am    Post subject: Re: Reading a string in VC++ Reply with quote

Thanks guys

Would you know how to do this using CString class.

Thanks
Back to top
Sharad Kala
Guest





PostPosted: Sat Mar 06, 2004 6:17 am    Post subject: Re: Reading a string in VC++ Reply with quote


"Rajeev" <navvyus (AT) yahoo (DOT) com> wrote

Quote:
Thanks guys

Would you know how to do this using CString class.

Yes, people might be knowing so but it is offtopic here.
It's an MFC class, try asking in microsoft.public.* newsgroups.
<OT>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_m
fc_cstring_class_members.asp
</OT>



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.