Skip to Content
author's profile photo Former Member
Former Member

GetInstallPath not working in C#

I have declared The GetInstallPath function in C# like this:


public static extern long GetInstallPath(string installDataFile, string outStr, long pathLength);

I call the declared function like this:

sapInstallPath = sapInstallPath.PadLeft(1024, ' ');

installDataFile = Application.StartupPath + @"\SBOAddOnRegData.sld";

GetInstallPath(installDataFile, sapInstallPath, sapInstallPath.Length);

But the sapInstallPath is always blank. Is there something incorrect in my declaration? Or my call?

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Jul 20, 2004 at 05:51 PM

    One reason could be that Microsoft has changed the definition of long in .Net. A variable of type long in VB6 is equivalent to type integer in .Net (no matter what languages you use)

    Try using integer instead of long. Passing a long means passing a value which probably is too large in size.


    public static extern integer GetInstallPath(string installDataFile, string outStr, integer pathLength);

    HTH Lutz Morrien

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 20, 2004 at 09:02 PM

    To use GetInstallPath you need to call the function like this:

    [DllImport(@"C:\Program Files\SAP Manage\SAP Business One SDK\UI API\Tools\SBOAddonReg.dll", EntryPoint="GetInstallPath", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]

    private static extern long GetInstallPath([MarshalAs(UnmanagedType.LPArray)] byte[] installDataFile,

    [MarshalAs(UnmanagedType.LPArray)] byte[] outStr,

    [MarshalAs(UnmanagedType.LPArray)]Int32[] lLen );

    In C#, dlls written in C++ are viewed as "unmanaged" code and you need to marshall the data if you require return values from any methods.

    To get the installation path, use the routine below:

    byte[] bPath = new byte[500];

    Int32[] lLen = new Int32[1];

    lLen[0] = 20;

    //Get the sPath where to install the addon exe

    string InstallStrFile = RegDir + @"\SBOAddOnRegData.sld";

    byte[] InstallFile = new byte[InstallStrFile.Length];

    InstallFile = System.Text.UTF8Encoding.ASCII.GetBytes(InstallStrFile);

    long Tmp = GetInstallPath(InstallFile, bPath, lLen);

    //convert the byte array back into a useable string - this returns a string with

    //the same length as the array. The end of the string contains \0\0\0 characters

    string InstallPath = System.Text.Encoding.ASCII.GetString(bPath);

    //get the actual size of the string

    int i = 0;

    for (i = 0; i < bPath.Length; i++)


    if (bPath<i> == 0)



    //set the string to a usaable form - this removes all the \0\0 at the end of the string

    InstallPath = InstallPath.Substring(0, i);

    The routine is rather long winded but it does work.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.