PC Review


Reply
Thread Tools Rate Thread

DllImport Attribute Issue

 
 
=?Utf-8?B?U0FM?=
Guest
Posts: n/a
 
      3rd May 2007
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,

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      3rd May 2007
On May 3, 2:33 pm, SAL <S...@discussions.microsoft.com> wrote:

<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

 
Reply With Quote
 
=?Utf-8?B?U0FM?=
Guest
Posts: n/a
 
      3rd May 2007
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.com> wrote:
>
> <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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DllImport issue! chandan Microsoft Dot NET Framework Forms 3 1st Oct 2007 06:58 AM
DllImport issue! chandan Microsoft C# .NET 1 29th Sep 2007 01:41 PM
DllImport attribute =?Utf-8?B?RG1pdHJ5IERvcm9iaW4=?= Microsoft Dot NET Framework 2 4th Apr 2007 02:52 PM
[DLLImport] Attribute where is default dll location =?Utf-8?B?TGVubg==?= Microsoft C# .NET 6 30th Dec 2005 05:54 PM
C++ DLLImport issue =?Utf-8?B?amVmZg==?= Microsoft Dot NET Compact Framework 0 20th Jun 2005 05:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:40 PM.