473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DllImport Attribute Issue

Hello,

I am getting the following error:

"An attribute argument must be a constant expression, typeof expression or
array creation expression"

with this line of code on my GetDLLName() that gets the DllName parameter
for the DllImport:

[DllImport(GetDLLName(), EntryPoint = "Logon", CallingConvention =
CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int Logon(ref LOGON_INFO rvLogon);

Here is the GetDLLName() function:

private static string GetDLLName()
{
string DLLName = string.Empty;

#if (DEBUG)
//Constant to store the value for the WIN32 DLL
supplied by Teledirect
//Assumes the DLL will reside in the
C:\WINNT\System32 directory
DLLName = "(''" +
@Environment.SystemDirectory.ToString() + @"\myDLL.dll" + "'')";
#else
//Constant to store the value for the WIN32 DLL
supplied by Teledirect
//Assumes the DLL will reside in the
C:\WINNT\System32 directory
DLLName = "(''" +
@Environment.SystemDirectory.ToString() + @"\myDLL.dll" + "'')";
#endif

return DLLName.ToString();
}

I have not used the DllImport Attribute before, but what I can see it takes
a String value for the DllName, and a set of NamedParameters that are
optional.

Can anyone tell me what I am doing wrong and how to correct it?

Thanks,

May 3 '07 #1
2 6107
On May 3, 2:33 pm, SAL <S...@discussions.microsoft.comwrote:

<snip>
I have not used the DllImport Attribute before, but what I can see it takes
a String value for the DllName, and a set of NamedParameters that are
optional.

Can anyone tell me what I am doing wrong and how to correct it?
As the compiler error says, the value of an attribute argument (the
DLL name in your case) must be a constant. That rules out using the
result of a method call.

If your method just chose between two constants based on DEBUG, you
could just have two constants - but it's using the system directory,
which is dynamic.

(To understand why this all needs to be constant - the value gets
compiled directly into the assembly as a constant - there's no runtime
initialization.)

Jon

May 3 '07 #2
Thanks Jon. That's what I didn't understand. When I looked here:

http://msdn2.microsoft.com/en-us/lib...attribute.aspx

It didn't specify that my DllName had to be a constant so I assumed it could
change.

"Jon Skeet [C# MVP]" wrote:
On May 3, 2:33 pm, SAL <S...@discussions.microsoft.comwrote:

<snip>
I have not used the DllImport Attribute before, but what I can see it takes
a String value for the DllName, and a set of NamedParameters that are
optional.

Can anyone tell me what I am doing wrong and how to correct it?

As the compiler error says, the value of an attribute argument (the
DLL name in your case) must be a constant. That rules out using the
result of a method call.

If your method just chose between two constants based on DEBUG, you
could just have two constants - but it's using the system directory,
which is dynamic.

(To understand why this all needs to be constant - the value gets
compiled directly into the assembly as a constant - there's no runtime
initialization.)

Jon

May 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: k | last post by:
Hiya, I'm hoping somebody can help me out here, I have searched for this problem but had no joy in finding a solution. I'm trying to use the DllImport attribute as follows: I have...
6
by: Lenn | last post by:
Hi, I have a DLL with unmanaged function written in C. I need to call from managed C# code, where should DLL have to placed, bin folder system32? And can this location be changed. I haven't...
6
by: Steve | last post by:
I'm sure I'm missing something basic. I have an class library that uses an unmanaged dll. Let's say the class library is called "CL1.dll" and is located at: and the unmanaged dll is called...
2
by: nospam | last post by:
Hello - I am getting a compile error "Expected class, delegate, enum". I am trying to create a Class Library in C# which wraps around an unmanaged DLL that manages a MIDI interface. In my...
1
by: Thomas Due | last post by:
Hi, I manage an rather old application in which we have some fairly complex (ugly) Delphi code. This is Delphi 6 we're talking about. Among all this Delphi code there is method for formating a...
2
by: Ed | last post by:
Hello, dear all, I often see these two import usage in the code. Both are the interface to use the Dll library. I think they are the same. Normally P/Invoke means using the to import the dll....
0
by: chandan | last post by:
Hi All, I have a dll created using vc++(Unmanaged).So when we are using this I have to give to full path of this dll as a parameter of DllImport attribute. The parameter takes only const string...
0
by: chandan | last post by:
Hi All, I have a dll created using vc++(Unmanaged).So when we are using this I have to give to full path of this dll as a parameter of DllImport attribute. The parameter takes only const string...
1
by: Larry | last post by:
I have an asp.net application (C#) that calls a web service that uses several c++ dlls which are loaded with DllImport. I put these dlls in a sub-directory off of the bin directory. For example,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.