Как получать ссылку из адресной строки оперы в textBox - C#
Формулировка задачи:
Как получать ссылку из адресной строки оперы в textBox? (для блокирования рекламы)
Решение задачи: «Как получать ссылку из адресной строки оперы в textBox»
textual
Листинг программы
//#define BINARY_DDE
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication29
{
public partial class frmMain : Form
{
const uint DMLERR_NO_ERROR = 0;
const uint CF_TEXT = 0x1;
const uint XCLASS_DATA = 0x2000;
const uint XTYP_REQUEST = 0x00B0 | XCLASS_DATA;
[Flags]
enum DdeFlags : uint
{
APPCLASS_STANDARD = 0x0,
APPCMD_CLIENTONLY = 0x10,
// more flags...
}
[DllImport( "user32.dll", SetLastError = true, CharSet = CharSet.Unicode )]
static extern uint DdeInitialize (
[In, Out] ref uint pidInst,
[In] IntPtr pfnCallback, // zero, do not used now
[In] DdeFlags afCmd,
[In] uint ulRes // reserved
);
[DllImport( "user32.dll", SetLastError = true, CharSet = CharSet.Unicode )]
static extern IntPtr DdeCreateStringHandle (
[In] uint idInst,
[In] string psz,
[In] int iCodePage
);
[DllImport( "user32.dll", SetLastError = true )]
[return: MarshalAs( UnmanagedType.Bool )]
static extern bool DdeFreeStringHandle (
[In] uint idInst,
[In] IntPtr hsz
);
[DllImport( "user32.dll", SetLastError = true )]
static extern IntPtr DdeConnect (
[In] uint idInst,
[In] IntPtr hszService,
[In] IntPtr hszTopic,
[In, Optional] IntPtr pCC // zero, do not used now
);
[DllImport( "user32.dll", SetLastError = true )]
[return: MarshalAs( UnmanagedType.Bool )]
static extern bool DdeDisconnect (
[In] IntPtr hConv
);
[DllImport( "user32.dll", SetLastError = true )]
static extern IntPtr DdeClientTransaction (
[In, Optional] byte[] pData,
[In] int cbData,
[In] IntPtr hConv,
[In, Optional] IntPtr hszItem,
[In] uint wFmt,
[In] uint wType,
[In] int dwTimeout,
[Out, Optional] IntPtr pdwResult // zero, do not used now
);
[DllImport( "user32.dll", SetLastError = true )]
static extern uint DdeGetData (
[In] IntPtr hData,
[Out] byte[] pDst,
[In] int cbMax,
[In] int cbOff
);
[DllImport( "user32.dll", SetLastError = true, EntryPoint = "DdeGetData" )]
static extern uint OperaDdeGetData (
[In] IntPtr hData,
[Out] StringBuilder pDst,
[In] int cbMax,
[In] int cbOff
);
[DllImport( "user32.dll", SetLastError = true )]
[return: MarshalAs( UnmanagedType.Bool )]
static extern bool DdeUninitialize (
[In] uint idInst
);
public frmMain ( )
{
InitializeComponent();
}
private void button1_Click ( object sender, EventArgs e )
{
uint pidInst = 0;
uint dwError;
IntPtr hszAppName,
hszTopic;
IntPtr hConn;
if ( (dwError = DdeInitialize(
ref pidInst, IntPtr.Zero,
DdeFlags.APPCLASS_STANDARD | DdeFlags.APPCMD_CLIENTONLY,
0 )) != DMLERR_NO_ERROR )
{
MessageBox.Show( "DdeInitializeW failed" );
return;
}
hszAppName = DdeCreateStringHandle( pidInst, "Opera", 0 );
hszTopic = DdeCreateStringHandle( pidInst, "WWW_GetWindowInfo", 0 );
hConn = DdeConnect( pidInst, hszAppName, hszTopic, IntPtr.Zero );
DdeFreeStringHandle( pidInst, hszAppName );
DdeFreeStringHandle( pidInst, hszTopic );
if ( hConn != IntPtr.Zero )
{
IntPtr hszItem;
IntPtr hData;
hszItem = DdeCreateStringHandle(
pidInst,
"0xFFFFFFFF", // current active tab in Opera
0
);
hData = DdeClientTransaction(
null, 0, hConn, hszItem,
CF_TEXT, XTYP_REQUEST, 0,
IntPtr.Zero
);
if ( hData != IntPtr.Zero )
{
#if BINARY_DDE
var buffer = new byte[512];
if ( DdeGetData( hData, buffer, buffer.Length, 0 ) != 0 )
MessageBox.Show( Encoding.Default.GetString( buffer ) );
else
MessageBox.Show( "DdeGetData failed..." );
#else
var sb = new StringBuilder( 512 );
if ( OperaDdeGetData( hData, sb, sb.Capacity, 0 ) != 0 )
MessageBox.Show( sb.ToString() );
else
MessageBox.Show( "DdeGetData failed..." );
#endif
}
DdeFreeStringHandle( pidInst, hszItem );
DdeDisconnect( hConn );
}
DdeUninitialize( pidInst );
}
}
}