Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi... i m getting this(Attribute DllImport is not valid on this declaration type. It is only valid on method declarations) error after calling a dll....

please see following code



<pre lang="cs">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Data.OleDb;

namespace EmpDetail
{

public partial class Form1 : Form
{

[DllImport("AkrutiEngine.dll")]

static int a;
Posted
Updated 10-Mar-13 19:25pm
v2

1 solution

Can you really read the error message? This is what it is. You are trying to apply the DllImport attribute to a field, which makes no sense whatsoever.

Technically, it is implemented through application of another attribute, System.AttributeUsageAttribute to this attribute, to limit the targets of some attributes:
http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx[^].

You can apply the attribute System.Runtime.InteropServices.DllImportAttribute only to methods, as you should have understood from your error message:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^].

I doubt you have a clue why using DllImport. To learn about it, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be used: http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900