Converting to Unicode

I have the following code in vb6:

StrConv(sServer, vbUnicode)

Can anybody tell me how to do this in c#


Answer this question

Converting to Unicode

  • Tracy Cai

    I do prefix the name with \\. It still wont close the connection though My code is as I pasted it previously, and when I replace the servername with null, it works fine as it loooks at the local machine. As soon as I pass the server name, it does not work but it works fine from the vb code
  • Bob Abraham

    You shouldn't need to do any conversion, having a look at the PInvoke signature on pinvoke.net (http://pinvoke.net/default.aspx/netapi32/NetFileClose.html), it just takes a standard string, .NET will worry about the marshalling.

    The server name that you pass to NetFileClose requires that it be prefixed with "\\", are you doing this

    If you are hardcoding the server name within the code, remember that you will need to escape the backslashes, ie:

    string name = "\\\\MyServer";      // Represents \\MyServer

    or

    string name = @"\\MyServer";      // Represents \\MyServer



  • MattFunke

    Hello Gbez,

    While I haven't tried running your code, I would recommend running your application with CLRSpy - specifically the Marshalling Customer Debug Probe. It may be that there is a marshalling issue with the PInvoke calls.

    Adam Nathan talks about CLR Spy on his CLR Interop Blog on MSDN: http://blogs.msdn.com/adam_nathan/archive/2004/01/13/58308.aspx

    Hope that helps,
    Stephen
    http://blogs.msdn.com/stfisher

  • gmeadows

    There is no equivalent in .NET, as it is not needed.

    Strings in .NET are always stored as Unicode and therefore there is no need to convert an existing string (already stored in Unicode) to Unicode.

    Depending on what you are trying to achieve in the bigger picture, you may need to look at the members of the System.Text.Encoding class.

  • pushpendra singh

    What I am actually trying to do is make an API call (NetFileClose) which takes the server name as one of the parameters. I have sample vb6 code that does this and they perform the function on the server name before they pass it as the parameter. I try to pass through the server name as the string with out doing any sort of conversion on it but it wont close any of the files. The call worls fine if I dont pass through the parameter (it sees it as the local machine), but as soon as I pass a server name thorugh it will not work. I have the permissions I need to close the file so I know that is not the case. It is definately something to do with the way that I pass the parameter through. Here is the vb6 code and my (c#) code:

    vb6:

    NetFileClose StrConv(sServer, vbUnicode), fi3.fi3_id

    c#:

    NetFileClose(strServer, pCurrent.fi3_id);

    I did try this as well but to no avail:

    byte[] data = new System.Text.UnicodeEncoding().GetBytes(strServer);

    NetFileClose(data.ToString(), pCurrent.fi3_id);



  • Thyagarajan

    Post all the code that you are using, and I will see if I can see if anything is wrong.



  • acangialosi

    const int MAX_PREFERRED_LENGTH = -1;

    int dwReadEntries;

    int dwTotalEntries;

    IntPtr pBuffer = IntPtr.Zero ;

    FILE_INFO_3 pCurrent = new FILE_INFO_3();

    string strServer = @"\\Javanew1.osiris.net";

    //get list of open files

    int dwStatus = NetFileEnum(strServer, null, null, 3, ref pBuffer, MAX_PREFERRED_LENGTH, out dwReadEntries, out dwTotalEntries, IntPtr.Zero );

    if (dwStatus == 0)

    {

    for (int dwIndex=0; dwIndex < dwReadEntries; dwIndex++)

    {

    IntPtr iPtr = new IntPtr(pBuffer.ToInt32() + (dwIndex * Marshal.SizeOf(pCurrent)));

    pCurrent = (FILE_INFO_3) Marshal.PtrToStructure(iPtr, typeof(FILE_INFO_3));

    lvAssets.Items.Add((string)(pCurrent.fi3_pathname));

    if (pCurrent.fi3_pathname.Substring(pCurrent.fi3_pathname.Length-4, 4) == ".doc")

    {

    NetFileClose(strServer, pCurrent.fi3_id);

    Globals.WriteLog("!!! User Disconnected" + " " + pCurrent.fi3_username + " " + pCurrent.fi3_id + " " + pCurrent.fi3_permission + " " + pCurrent.fi3_pathname, Globals.strCubeLogFile);

    sbStatus.Text = "User Disconnected" + " " + pCurrent.fi3_username + " " + pCurrent.fi3_id + " " + pCurrent.fi3_permission + " " + pCurrent.fi3_pathname;

    }

    }

    NetApiBufferFree(pBuffer);

    }


  • Converting to Unicode