Reading a string in VC++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rajeev

    Reading a string in VC++

    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("D OMAIN", 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_CONNE CTED = 2250&
    Const ERROR_MORE_DATA = 234&
    Const ERROR_NO_NETWOR K = 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(strNWUserNa me)
    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=Con tainer -Login
    If strNWUserName Like ".CN=*.O=*" Then
    lngSecondDotPos = InStr(1, strNWUserName, ".O=", vbTextCompare)
    NWUserName = Mid$(strNWUserN ame, 5, lngSecondDotPos - 5)
    Else
    Err.Raise vbObjectError Or 1001, "NWUserName ", _
    "Not a Netware login name."
    End If
    Else
    Select Case lngResult
    Case ERROR_NOT_CONNE CTED
    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_NETWOR K
    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
  • John Harrison

    #2
    Re: Reading a string in VC++


    "Rajeev" <navvyus@yahoo. com> wrote in message
    news:8fa6add7.0 403041719.34320 53d@posting.goo gle.com...[color=blue]
    > 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("D OMAIN", 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.[/color]

    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("D OMAIN", strNWUserName, &dBufferLength) ;
    string user_name(strNW UserName);
    if (user_name.comp are(0, 4, ".CN=") != 0)
    {
    // not a NW username
    return;
    }
    string::size_ty pe pos = user_name.find( ".O=", 4);
    if (pos == string::npos)
    {
    // not a NW username
    return;
    }
    string nw_user_name = user_name.subst r(4, pos - 4);

    john


    Comment

    • Chris \( Val \)

      #3
      Re: Reading a string in VC++


      "Rajeev" <navvyus@yahoo. com> wrote in message
      news:8fa6add7.0 403041719.34320 53d@posting.goo gle.com...
      | 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("D OMAIN", 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::si ze_type Start( Source.find( Token ) );

      std::string::si ze_type End(
      Source.find_fir st_of( '.', Start + Token.size() ) );

      if( Start != std::string::np os && End != std::string::np os )
      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


      Comment

      • Rajeev

        #4
        Re: Reading a string in VC++

        Thanks guys

        Would you know how to do this using CString class.

        Thanks

        Comment

        • Sharad Kala

          #5
          Re: Reading a string in VC++


          "Rajeev" <navvyus@yahoo. com> wrote in message
          news:8fa6add7.0 403051633.14e5d da0@posting.goo gle.com...[color=blue]
          > Thanks guys
          >
          > Would you know how to do this using CString class.[/color]

          Yes, people might be knowing so but it is offtopic here.
          It's an MFC class, try asking in microsoft.publi c.* newsgroups.
          <OT>

          fc_cstring_clas s_members.asp
          </OT>


          Comment

          Working...