Calling a local C# Dll from Javascript!

A

Adrian

Hi
I want to write a simple DLL in C# that I can call from a local html
page using JavaScript. I'm looking for a very simple example!

The sort of think I'm looking for is from JS to call the DLL and it return
the date for example, or even just a string "abc".

Ideally I would like to see both the C# code and JS code!

Anyone help me!
 
A

Adrian

Isn't it funny how the JS groups say it a C# issue! and visa virsa, but
those with the comments know not the anwsers, or choose not to share!

Surly as there is detail of how to develop this in C# and referance is made
to using it from JS, its not unresonable to ask especialy as I don't know if
the C# DLL would or would not work from JS without a way of testing it and
point is to develop a C# DLL that can be called from a local HTML page in MS
IE6 running on XP...
 
W

William DePalo [MVP VC++]

Adrian said:
Isn't it funny how the JS groups say it a C# issue! and visa virsa, but
those with the comments know not the anwsers, or choose not to share!

:)

This is not an issue of deliberately not sharing information. It is not in
the interest of anyone from MS or any of the news group volunteers to keep
anything from you.

Rather, the pointer to another newsgroup serves to do two things. First, it
increases the likelihood that you will be put in contact with someone who
can help you. And second, it keeps the group in which the post was made "on
topic".

The fact that it often takes a knowledge of two or three technolgies to
accomplish a given task while newsgroup regulars tend to be masters of one
only further complicates matters.

That said, I have to tell you that I am not a JScript expert. But, were I
you, I would look at the JScript function ActiveXObject used like so

newObj = new ActiveXObject(servername.typename[, location])

It is described here:

http://msdn2.microsoft.com/en-US/library/6958xykx.aspx

That function allows you to create a COM object in script code.

Then what you need to do is to figure out how to make your C# class appear
to clients as a COM object. One way to do that is to use the RegAsm
(register assembly) tool to register a .Net assembly as a COM object. It is
described here:

http://msdn.microsoft.com/library/d...ml/cpgrfAssemblyRegistrationToolRegasmexe.asp

Now, I'm neither an expert at scripting or COM - you'll find one of those
characters in another group. There may be other hurdles involved (security?)
in the solution I sketched and in fact there may be a better method. Good
luck.

Regards,
Will
 
C

Carl Daniel [VC++ MVP]

Adrian said:
Isn't it funny how the JS groups say it a C# issue! and visa virsa,

No doubt, but this is a C++ newsgroup, not C# nor JScript.

You need to expose your C# class as a COM callable object. See the
COMVisibleAttribute in the .NET documentation.

You need to regisster your C# DLL as a COM server - see regasm.exe in the
..NET framework SDK (you should already have it if you have a C# compiler).

If you're going to access your C# COM DLL from a web page (i.e. ASP), you'll
need to strong-name your assembly (see sn.exe in the .NET framework) and put
it in the GAC (see gacutil.exe in the .NET framework).

This article

http://www.codeproject.com/dotnet/cominterop.asp

contains a lot of information on COM/.NET interop, which is what you need to
understand to mix JScript and C#.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top