
8.14.2004
Platform Invoke (Interop) Info
A buddy was asking me if you can use Platform Invoke (P-Invoke) from an asp.net page if you are not using a code-behind .cs file (as created by the vs.net app wizard)?
update -- got it working using a .cs code behind file
I couldn't get it working in only a .aspx file. I moved the p-invoke part to a .cs file that the page inherits from (dynamically compiles this file on demand). Here's how:
Note the inherits and src attributes in the page element. They point to the Interop class in the interop.cs file. This file sits in the same dir as the .aspx page on the web server. ASP.NET compiles this source file on the fly whenever a change is detected.
In interop.cs:
Sweet! The tickcount is displayed on the page!!
I played around with it, but can't get it to work. I don't find anything quickly on the web that says you can not, but looks like the asp.net compiler doesn't like definitions of a class in the .aspx file (in the script terd).
You can definitely do this from a .cs file. The problem seems to be that do declare the Win32 API that you want to call, you end up declaring a class with the DllImport attribute. This works fine in .cs file, but not so much in a .aspx file in a script terd.
tried something like this, but can't get it to work:
When cruising the net for an answer, came across this tip sheet all about p-invoke.
Platform Invoke Cheat Sheet
update -- got it working using a .cs code behind file
I couldn't get it working in only a .aspx file. I moved the p-invoke part to a .cs file that the page inherits from (dynamically compiles this file on demand). Here's how:
<%@ page language="c#" inherits="Interop" src="interop.cs" %%gt;
<%
Response.Write( GetTicks() );
%%gt;
Note the inherits and src attributes in the page element. They point to the Interop class in the interop.cs file. This file sits in the same dir as the .aspx page on the web server. ASP.NET compiles this source file on the fly whenever a change is detected.
In interop.cs:
using System;
using System.Web;
using System.Web.UI;
using System.Reflection;
using System.Runtime.InteropServices;
public class Interop : Page
{
protected string Hello()
{
return "hello world" ;
}
protected ulong GetTicks()
{
return Win32.GetTickCount();
}
}
public class Win32
{
[DllImport("kernel32.dll")]
public static extern ulong GetTickCount();
}
Sweet! The tickcount is displayed on the page!!
I played around with it, but can't get it to work. I don't find anything quickly on the web that says you can not, but looks like the asp.net compiler doesn't like definitions of a class in the .aspx file (in the script terd).
You can definitely do this from a .cs file. The problem seems to be that do declare the Win32 API that you want to call, you end up declaring a class with the DllImport attribute. This works fine in .cs file, but not so much in a .aspx file in a script terd.
tried something like this, but can't get it to work:
<%@ Page language="c#" %>
<%@ import namespace="System.Runtime.InteropServices" %>
<%
public class Win32 {
[DllImport("kernel32.dll")]
public static extern unsigned long GetTickCount();
};
int x = 1;
x = Win32.GetTickCount();
Response.Write( x );
%>
When cruising the net for an answer, came across this tip sheet all about p-invoke.
Platform Invoke Cheat Sheet
Comments:
Could you please update the link to Platform Invoke Cheat Sheet, or just give an example/pointers on how to avoid the code-behind cs file?
I can't find it myself, while "The GotDotNet site has been shut down."..
I can't find it myself, while "The GotDotNet site has been shut down."..
(my own goal is to avoid OOP completely, if possible, since I'm about to write a sample app only - it'd be better to "publish" the easiest-to-understand version)
Post a Comment