cancel
Showing results for 
Search instead for 
Did you mean: 

GetInstallPath not working in C#

Former Member
0 Kudos

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

[DllImport("SBOAddonReg.dll")]

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?

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

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)

break;

}

//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.

Former Member
0 Kudos

thanks Lita. that worked just fine.

Former Member
0 Kudos

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.

[DllImport("SBOAddonReg.dll")]

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

HTH Lutz Morrien