GLOBEtech


Call Managed C# Method from JavaScript Code

After searching quite long to find a way to call a C# Method from any JavaScript Code which is hostet in a WinForms Webbrowser Control, I found this one here:

C# Code
[ComVisible(true)]
public class ScriptManager
{
Form mForm;

public ScriptManager(Form form)
{
mForm= form;
public void MethodToCallFromScript()
{
mForm.DoSomething()
}
}
}

public class MyForm : Form
{
private Webbrowser mBrowserCtrl;

public MyForm()

{

mBrowserCtrl = new Webbrowser();
mBrowserCtrl.ObjectForScripting= new ScriptManager(this);

}

public void DoSomething(){}

}

JavaScript Code
function HandleSomething()
{
window.external.MethodToCallFromScript();
}

Advertisements

Playing Sounds in Windows Mobile

After working a bit with the new Windows Mobile 6 SDK i tried to play sounds located on the device. Since there are a few tricky steps, here is the code (C#):


using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SoundMachine
{
public class Sound
{
[DllImport(“CoreDll.dll”, EntryPoint = “PlaySound”, SetLastError = true)]
private extern static int PlaySound(string szSound, IntPtr hMod, int flags);

private static string soundLocation = @”sounds\”;

private enum SND
{
SYNC = 0x0000,
ASYNC = 0x0001,
NODEFAULT = 0x0002,
MEMORY = 0x0004,
LOOP = 0x0008,
NOSTOP = 0x0010,
NOWAIT = 0x00002000,
ALIAS = 0x00010000,
ALIAS_ID = 0x00110000,
FILENAME = 0x00020000,
RESOURCE = 0x00040004
}

public static void PlaySound(string fileName)
{
PlaySound(Sound.soundLocation + fileName, IntPtr.Zero, (int)(SND.SYNC | SND.FILENAME));
}
}

Its that easy 🙂