Sepp
Sepp

Reputation: 63

Error when trying to replace an UNC path by a string

Sorry for some maybe very basic questions. I simply wanted to replace the UNC path by a string. These lines with the UNC path for a C++ connection perfectly works:

[DllImport(C:\\Users\\SJ\\Documents\\VS2015\\Projects\\P_01\\Debug\\EV_01.dll",
EntryPoint = "DDentry", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void DDentry
(
   [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
    string[,] pArrayStr
);

Replacing the UNC path by a string gives an error "An object reference is required for the non-static field, method, or property"

string UNCpath = @"C:\\Users\\SJ\\Documents\\VS2015\\Projects\\P_01\\Debug\\EV_01.dll";

[DllImport(UNCpath,
EntryPoint = "DDentry", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void DDentry
(
   [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
    string[,] pArrayStr
);

Many thank for your ideas ..

Upvotes: 0

Views: 120

Answers (2)

You are trying to use a non-constant string with an attribute and it isn't allowed. You must declare your string as "const".

Upvotes: 0

Tim
Tim

Reputation: 6060

You cannot pass the instance value UNCPath into an attribute like that. It would need to be a constant. Also, if you use the double-backslash escape sequences, you can't use the @ prefix to the string.

Try this:

const string UNCpath = "C:\\Users\\SJ\\Documents\\VS2015\\Projects\\P_01\\Debug\\EV_01.dll";

[DllImport(UNCpath,
EntryPoint = "DDentry", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern void DDentry
(
   [MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
    string[,] pArrayStr
);

Upvotes: 1

Related Questions