OnClick inside CAppDialog not handle CBmpButton Clicks - mql4

I am trying to create a Dialog with two CBmpButtons. The UX works fine BUT when the programs needs to handle the button clicks just detects the first button and no the other one. Am I doing something wrong in here? I have made another Panel using CAppDialog and the code handles all the button clicks perfectly but not in this one. Thanks in advance.
This is my code:
Class
#include <Controls\Dialog.mqh>
#include <Controls\BmpButton.mqh>
class Tester : public CAppDialog
{
private:
CBmpButton btn_trendline;
CBmpButton btn_crosshair;
public:
Tester(void) {};
~Tester(void) {}
virtual bool Create(const long chart, const string name, const int subwin, const int x1, const int y1, const int x2, const int y2);
virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);
protected:
bool CreateTLBtn();
bool CreateCHBtn();
void OnClickedTLBtn();
void OnClickedCHBtn();
};
EVENT_MAP_BEGIN(Tester)
ON_EVENT(ON_CLICK,btn_crosshair,OnClickedCHBtn)
ON_EVENT(ON_CLICK,btn_trendline,OnClickedTLBtn)
EVENT_MAP_END(CAppDialog)
bool Tester::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
{
if ( !CAppDialog::Create(chart, name, subwin, x1, y1, x2, y2) )
return false;
if ( !CreateCHBtn() )
return false;
if ( !CreateTLBtn() )
return false;
return true;
}
bool Tester::CreateCHBtn(void)
{
if ( !btn_crosshair.Create(0, "crosshair_btn", 0, 5, 3, 15, 15) )
return false;
btn_crosshair.BmpNames("\\Images\\ic_test_off.bmp","\\Images\\ic_test_on.bmp");
btn_trendline.Locking(true);
if ( !Add(btn_crosshair) )
return false;
return true;
}
bool Tester::CreateTLBtn(void)
{
if ( !btn_trendline.Create(0, "trendline_btn", 0, 40, 3, 15, 15) )
return false;
btn_trendline.BmpNames("\\Images\\ic_test_off.bmp","\\Images\\ic_test_on.bmp");
btn_trendline.Locking(true);
if ( !Add(btn_trendline) )
return false;
return true;
}
void Tester::OnClickedCHBtn(void)
{
Print("CH");
}
void Tester::OnClickedTLBtn(void)
{
Print("TL");
}
Indicator:
#include <#indicesGodd\Test.mqh>
Tester tester;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
if (!tester.Create(0, "Probando", 0, 5, 5, 300, 150))
return(INIT_FAILED);
if ( !tester.Run() )
return(INIT_FAILED);
//---
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
tester.Destroy(reason);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
//---
//--- return value of prev_calculated for next call
return(rates_total);
}
void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
{
tester.ChartEvent(id, lparam, dparam, sparam);
}
//+------------------------------------------------------------------+

Related

How to disable auto group codo in one line for a short function?

int compare (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
The above code is indented like this by the following command. I'd like to keep its original style (if it is one line, the output is one line, if the input is three lines, the output should be three lines.) Is there a way to do so with clang-format?
$ clang-format -style='{IndentWidth: 8, UseTab: Always, SpaceBeforeParens: Never, IndentCaseLabels: true }'
int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
Given input.cpp:
int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); }
This is the result of using AllowShortFunctionsOnASingleLine: None:
% clang-format -style='{AllowShortFunctionsOnASingleLine: None}' input.cpp
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
% clang-format --version
7.0.1

How do I load an IStream into a TMemoryStream

How do I use a TComInterface<IStream> object and read it into a TMemoryStream? Can I take TComInterface<IStream> and use it as a TStream* somehow to use the TMemoryStream LoadFromStream call?
One way is to Read() the IStream data into a buffer that you then Write() to the TMemoryStream, eg:
TComInterface<IStream> pIStrm;
TMemoryStream *pMStrm;
...
STATSTG stat = {0};
OleCheck(pIStrm->Stat(&stat, STATFLAG_NONAME));
LARGE_INTEGER li;
li.QuadPart = 0;
ULARGE_INTEGER ul;
OleCheck(pIStrm->Seek(li, STREAM_SEEK_CUR, &ul));
unsigned __int64 ulPos = ul.QuadPart;
BYTE buffer[1024];
while (ulPos < stat.cbSize.QuadPart)
{
ULONG ulRead;
OleCheck(pIStrm->Read(buffer, min(stat.cbSize.QuadPart - ulPos, sizeof(buffer)), &ulRead));
pMStrm->WriteBuffer(buffer, ulRead);
ulPos += ulRead;
}
...
Another option is to write a TStream-derived class that accesses the IStream internally (similar to how the RTL's TStreamAdapter class wraps a TStream so it can be passed around as an IStream), eg:
class TIStreamWrapper : public TStream
{
private:
TComInterface<IStream> pIStrm;
protected:
virtual __int64 __fastcall GetSize();
virtual void __fastcall SetSize(const __int64 NewSize);
public:
__fastcall TIStreamWrapper(IStream *Strm);
virtual int __fastcall Read(void *Buffer, int Count);
virtual int __fastcall Write(const void *Buffer, int Count);
virtual __int64 __fastcall Seek(const __int64 Offset, TSeekOrigin Origin);
};
__fastcall TIStreamWrapper::TIStreamWrapper(IStream *Strm)
: pIStrm(Strm, true)
{
}
__int64 __fastcall TIStreamWrapper::GetSize()
{
STATSTG stat = {0};
OleCheck(pIStrm->Stat(&stat, STATFLAG_NONAME));
return stat.cbSize.QuadPart;
}
void __fastcall TIStreamWrapper::SetSize(const __int64 NewSize)
{
ULARGE_INTEGER ul;
ul.QuadPart = NewSize;
OleCheck(pIStrm->SetSize(ul));
}
int __fastcall TIStreamWrapper::Read(void *Buffer, int Count)
{
ULONG ulRead;
OleCheck(pIStrm->Read(Buffer, Count, &ulRead));
return ulRead;
}
int __fastcall TIStreamWrapper::Write(const void *Buffer, int Count)
{
ULONG ulWritten;
OleCheck(pIStrm->Write(Buffer, Count, &ulWritten));
return ulWritten;
}
static const DWORD IStreamSeekOrigin[] = {STREAM_SEEK_SET, STREAM_SEEK_CUR, STREAM_SEEK_END};
__int64 __fastcall TIStreamWrapper::Seek(const __int64 Offset, TSeekOrigin Origin)
{
LARGE_INTEGER li;
li.QuadPart = Offset;
ULARGE_INTEGER ul;
OleCheck(pIStrm->Seek(li, IStreamSeekOrigin[Origin], &ul));
return ul.QuadPart;
}
TComInterface<IStream> pIStrm;
TMemoryStream *pMStrm;
...
TIStreamWrapper *pWrapper = new TIStreamWrapper(pIStrm);
try
{
pMStrm->LoadFromStream(pWrapper);
// or: pMStrm->CopyFrom(pWrapper, 0);
}
__finally
{
delete pWrapper;
}
...

Increase LabelField value as slider is moved

I have created a SliderField class in order to show a drag-able slider in my BlackBerry application. The slider is basically used to set interval for location updates (via GPS) in the app. The app is built on OS 5.0 and so no API was found for the SliderField. The problem
I am facing is that I need to increase the integer value in a label as the slider is moved. For instance, if the integer value in a label is 1 and the user moves the slider the value should increase to 5,10,15 etc. I do not know how to link moving the slider to increasing the value in the label field. Can anyone please help? I am adding the SliderField object to the screen as below:
Bitmap sliderBack = Bitmap.getBitmapResource( "progress51.png" );
Bitmap sliderFocus = Bitmap.getBitmapResource( "progress51.png" );
Bitmap sliderThumb = Bitmap.getBitmapResource( "butt.png" );
SliderField theSlider = new SliderField( sliderThumb, sliderBack, sliderFocus,20, 1, 1, 1 );
secondHFM.add(theSlider)
Below is my code for the SliderField class.
public class SliderField extends Field
{
Bitmap _imageThumb;
Bitmap _imageSlider;
Bitmap _imageSliderLeft;
Bitmap _imageSliderCenter;
Bitmap _imageSliderRight;
Bitmap _imageSliderFocus;
Bitmap _imageSliderFocusLeft;
Bitmap _imageSliderFocusCenter;
Bitmap _imageSliderFocusRight;
private int _numStates;
private int _currentState;
private boolean _selected;
private int _xLeftBackMargin;
private int _xRightBackMargin;
private int _thumbWidth;
private int _thumbHeight;
private int _totalHeight;
private int _totalWidth;
private int _rop;
private int _backgroundColours[];
private int _backgroundSelectedColours[];
private int _defaultSelectColour = 0x977DED;
private int _defaultBackgroundColour = 0x000000;
private int _defaultHoverColour = 0x999999;
public SliderField( Bitmap thumb
, Bitmap sliderBackground
, int numStates
, int initialState
, int xLeftBackMargin
, int xRightBackMargin )
{
this( thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE );
}
public SliderField( Bitmap thumb
, Bitmap sliderBackground
, int numStates
, int initialState
, int xLeftBackMargin
, int xRightBackMargin
, long style )
{
this( thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, style );
}
public SliderField( Bitmap thumb
, Bitmap sliderBackground
, Bitmap sliderBackgroundFocus
, int numStates
, int initialState
, int xLeftBackMargin
, int xRightBackMargin )
{
this( thumb, sliderBackground, sliderBackgroundFocus, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE );
}
public SliderField( Bitmap thumb
, Bitmap sliderBackground
, Bitmap sliderBackgroundFocus
, int numStates
, int initialState
, int xLeftBackMargin
, int xRightBackMargin
, long style )
{
super( style );
if( initialState > numStates || numStates < 2 ){
}
_imageThumb = thumb;
_imageSlider = sliderBackground;
_imageSliderFocus = sliderBackgroundFocus;
_numStates = numStates;
setState( initialState );
_xLeftBackMargin = xLeftBackMargin;
_xRightBackMargin = xRightBackMargin;
_rop = _imageSlider.hasAlpha() ? Graphics.ROP_SRC_ALPHA : Graphics.ROP_SRC_COPY;
_thumbWidth = thumb.getWidth();
_thumbHeight = thumb.getHeight();
initBitmaps();
}
public SliderField( Bitmap thumb
, Bitmap sliderBackground
, int numStates
, int initialState
, int xLeftBackMargin
, int xRightBackMargin
, int[] colours
, int[] selectColours )
{
this(thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE );
if( colours.length != numStates+1 ){
throw new IllegalArgumentException();
}
_backgroundColours = colours;
_backgroundSelectedColours = selectColours;
}
public void initBitmaps()
{
int height = _imageSlider.getHeight();
_imageSliderLeft = new Bitmap( _xLeftBackMargin, height );
_imageSliderCenter = new Bitmap( _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height);
_imageSliderRight = new Bitmap( _xRightBackMargin, height );
copy( _imageSlider, 0, 0, _xLeftBackMargin, height, _imageSliderLeft );
copy( _imageSlider, _xLeftBackMargin, 0, _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height, _imageSliderCenter);
copy( _imageSlider, _imageSlider.getWidth() - _xRightBackMargin, 0, _xRightBackMargin, height, _imageSliderRight);
_imageSliderFocusLeft = new Bitmap( _xLeftBackMargin, height );
_imageSliderFocusCenter = new Bitmap( _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height);
_imageSliderFocusRight = new Bitmap( _xRightBackMargin, height );
copy( _imageSliderFocus, 0, 0, _xLeftBackMargin, height, _imageSliderFocusLeft );
copy( _imageSliderFocus, _xLeftBackMargin, 0, _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height, _imageSliderFocusCenter);
copy( _imageSliderFocus, _imageSlider.getWidth() - _xRightBackMargin, 0, _xRightBackMargin, height, _imageSliderFocusRight);
}
private void copy(Bitmap src, int x, int y, int width, int height, Bitmap dest) {
int[] argbData = new int[width * height];
src.getARGB(argbData, 0, width, x, y, width, height);
for(int tx = 0; tx < dest.getWidth(); tx += width) {
for(int ty = 0; ty < dest.getHeight(); ty += height) {
dest.setARGB(argbData, 0, width, tx, ty, width, height);
}
}
}
public void setState(int newState) {
if( newState > _numStates ){
throw new IllegalArgumentException();
} else {
_currentState = newState;
invalidate();
}
}
public int getState() {
return _currentState;
}
public int getNumStates() {
return _numStates;
}
public int getColour() {
if(_backgroundSelectedColours != null) {
return _backgroundSelectedColours[getState()];
}
return 0x000000;
}
public int getPreferredWidth() {
return _totalWidth;
}
public int getPreferredHeight() {
return _totalHeight;
}
protected void layout( int width, int height ) {
if (width < 0 || height < 0)
throw new IllegalArgumentException();
_totalWidth = width;
_totalHeight = Math.max(_imageSlider.getHeight(), _imageThumb.getHeight());
setExtent( _totalWidth, _totalHeight );
}
public void paint( Graphics g )
{
int sliderHeight = _imageSlider.getHeight();
int sliderBackYOffset = ( _totalHeight - sliderHeight ) >> 1;
int backgroundColor = _defaultBackgroundColour;
if( _backgroundSelectedColours != null || _backgroundColours != null ) {
if( _selected ) {
backgroundColor = _backgroundSelectedColours != null ? _backgroundSelectedColours[getState()] : _defaultSelectColour;
} else if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) {
backgroundColor = _backgroundColours != null ? _backgroundColours[getState()] : _defaultHoverColour;
} else {
backgroundColor = _defaultBackgroundColour;
}
}
g.setColor( backgroundColor );
g.fillRect( 1, sliderBackYOffset + 1, _totalWidth - 2, sliderHeight - 2 );
if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) {
paintSliderBackground( g, _imageSliderFocusLeft, _imageSliderFocusCenter, _imageSliderFocusRight );
} else {
paintSliderBackground( g, _imageSliderLeft, _imageSliderCenter, _imageSliderRight );
}
int thumbXOffset = ( ( _totalWidth - _thumbWidth ) * _currentState ) / _numStates;
g.drawBitmap( thumbXOffset, ( _totalHeight - _thumbHeight ) >> 1, _thumbWidth, _thumbHeight, _imageThumb, 0, 0 );
}
private void paintSliderBackground( Graphics g, Bitmap left, Bitmap middle, Bitmap right )
{
int sliderHeight = _imageSlider.getHeight();
int sliderBackYOffset = ( _totalHeight - sliderHeight ) >> 1;
g.drawBitmap( 0, sliderBackYOffset, _xLeftBackMargin, sliderHeight, left, 0, 0 );
g.tileRop( _rop, _xRightBackMargin, sliderBackYOffset, _totalWidth - _xLeftBackMargin - _xRightBackMargin, sliderHeight, middle, 0, 0 );
g.drawBitmap( _totalWidth - _xRightBackMargin, sliderBackYOffset, _xRightBackMargin, sliderHeight, right, 0, 0 );
}
public void paintBackground( Graphics g )
{
}
protected void drawFocus( Graphics g, boolean on )
{
boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
try {
if( on ) {
g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
}
paint( g );
} finally {
g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
}
}
protected boolean touchEvent(TouchEvent message)
{
boolean isConsumed = false;
boolean isOutOfBounds = false;
int x = message.getX(1);
int y = message.getY(1);
if(x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {
isOutOfBounds = true;
}
switch(message.getEvent()) {
case TouchEvent.CLICK:
case TouchEvent.MOVE:
if(isOutOfBounds) return true;
_selected = true;
int stateWidth = getExtent().width / _numStates;
int numerator = x / stateWidth;
int denominator = x % stateWidth;
if( denominator > stateWidth / 2 ) {
numerator++;
}
_currentState = numerator;
invalidate();
isConsumed = true;
break;
case TouchEvent.UNCLICK:
if(isOutOfBounds) {
_selected = false;
return true;
}
_selected = false;
stateWidth = getExtent().width / _numStates;
numerator = x / stateWidth;
denominator = x % stateWidth;
if( denominator > stateWidth / 2 ) {
numerator++;
}
_currentState = numerator;
invalidate();
fieldChangeNotify(0);
isConsumed = true;
break;
}
return isConsumed;
}
protected boolean navigationMovement(int dx, int dy, int status, int time)
{
if( _selected )
{
if(dx > 0 || dy > 0) {
incrementState();
fieldChangeNotify( 0 );
return true;
} else if(dx < 0 || dy < 0) {
decrementState();
fieldChangeNotify( 0 );
return true;
}
}
return super.navigationMovement( dx, dy, status, time);
}
public void decrementState() {
if(_currentState > 0) {
_currentState--;
invalidate();
}
}
public void incrementState() {
if(_currentState < _numStates) {
_currentState++;
invalidate();
}
}
protected boolean invokeAction(int action) {
switch(action) {
case ACTION_INVOKE: {
toggleSelected();
return true;
}
}
return false;
}
protected boolean keyChar( char key, int status, int time ) {
if( key == Characters.SPACE || key == Characters.ENTER ) {
toggleSelected();
return true;
}
return false;
}
protected boolean trackwheelClick( int status, int time ) {
if( isEditable() ) {
toggleSelected();
return true;
}
return super.trackwheelClick(status, time);
}
private void toggleSelected() {
_selected = !_selected;
invalidate();
}
public void setDirty( boolean dirty )
{
}
public void setMuddy( boolean muddy )
{
}
}
If you use the code below, you will get your actual requirement,you can get the slider current value into your screen class by invoking getValue() method on sliderfield reference..
Implement FieldChangeListener interface and try to override fieldChanged() method.
Use HorizontalFieldManager class and setStatus() method,
if you want to display your slider bar at bottom of your screen,other wise you can display it normally.
Here the Code:
public class SliderScreen extends MainScreen implements FieldChangeListener
{
private SliderField slider;
private HorizontalFieldManager hm;
public SliderField(){
slider = new SliderField(
Bitmap.getBitmapResource("slider2_thumb_normal.png"),
Bitmap.getBitmapResource("slider2_progress_normal.png"),
Bitmap.getBitmapResource("slider2_base_normal.png"),
Bitmap.getBitmapResource("slider2_thumb_focused.png"),
Bitmap.getBitmapResource("slider2_progress_focused.png"),
Bitmap.getBitmapResource("slider2_base_focused.png"), 8, 4,
8, 8, FOCUSABLE);
slider.setChangeListener(this);
hm = new HorizontalFieldManager();
hm.add(slider);
setStatus(hm);
}
public void fieldChanged(Field field, int context) {
try {
if (field == slider) {
int value = slider.getValue();
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
I have done simple example of how to use this code,Check it
public final class MyScreen extends MainScreen implements FieldChangeListener//,FocusChangeListener
{
/**
* Creates a new MyScreen object
*/
public MyScreen()
{
// Set the displayed title of the screen
setTitle("MyTitle");
SliderField slider;
slider = new SliderField(
Bitmap.getBitmapResource( "slider_thumb_normal.png" ), Bitmap.getBitmapResource( "slider_progress_normal.png" ), Bitmap.getBitmapResource( "slider_base_normal.png" ),
Bitmap.getBitmapResource( "slider_thumb_focused.png" ), Bitmap.getBitmapResource( "slider_progress_focused.png" ), Bitmap.getBitmapResource( "slider_base_focused.png"),
Bitmap.getBitmapResource( "slider_thumb_pressed.png" ), Bitmap.getBitmapResource( "slider_progress_pressed.png" ), Bitmap.getBitmapResource( "slider_base_pressed.png"),
10, 0, 12, 12, FOCUSABLE );
slider.setPadding( 20, 20, 20, 20 );
slider.setBackground( BackgroundFactory.createSolidBackground( 0xD3D3D3 ) );
slider.setChangeListener(this);
//slider.focusChangeNotify(0);
add( slider );
}
public void fieldChanged(Field field, int context) {
// TODO Auto-generated method stub
if(field instanceof SliderField)
{
SliderField temp = (SliderField)field;
System.out.println("Temp value is"+temp.getValue());
}
}
}

argument of pthread_create()

We know that we call pthread like this
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void* arg);
However, if in the start_routine function I wanna call has more than one argument, what can I do?
You can put whatever you want into a struct and pass a pointer to that.
In C:
typedef struct {
int a;
int b;
} ChildMainArgs;
void child_main(int a,int b);
void child_main_thread(void *arg)
{
ChildMainArgs *args_ptr = (ChildMainArgs *)arg;
child_main(args_ptr->a,args_ptr->b);
}
ChildMainArgs args;
args.a = 5;
args.b = 7;
pthread_create(..,..,child_main_thread,&args);

How to disable a specific drive

I need to disable cdrom and the usb mass storage drivers using c# windows service. i have noticed there is a post Disable the use of CD drive (VB.NET) but it keeps ejecting the cdrom tray. does anyone know how to disable the cd rom other than using this method.????
On Vista and above you can use Group Policy settings to disable USB, CD-ROM and other types of removable storage. And you don't even need a service for that. If you cannot do this through group policy infrastructure, there is an option to set registry keys that Group Policy controls. Registry key locations are mentioned in this Vista forum post.
Check out VolumeManager defined below. It has functionality on locking volumes and disabling CD-ROM eject
public class VolumeManager
{
public string DriveLetter { private set; get; }
public IntPtr hFile { private set; get; }
public VolumeManager(string driveletter)
{
DriveLetter = driveletter;
}
public EjectState Eject()
{
if (!OpenHandle())
return EjectState.Failed;
if (Lock() && Dismount())
{
if (PreventRemoval(false) && AutoEject())
{
CloseHandle();
return EjectState.AutoEject;
}
else
{
CloseHandle();
return EjectState.RemoveSafely;
}
}
else
{
CloseHandle();
return EjectState.NoLock;
}
}
public bool OpenHandle()
{
string filename = "\\\\.\\" + DriveLetter + ":";
hFile = Win32.CreateFile(filename, Win32.GENERIC_READ | Win32.GENERIC_WRITE,
Win32.FILE_SHARE_READ | Win32.FILE_SHARE_WRITE, IntPtr.Zero, Win32.OPEN_EXISTING, 0, IntPtr.Zero);
if (hFile.ToInt32() == Win32.INVALID_HANDLE)
{
return false;
}
return true;
}
public void CloseHandle()
{
Win32.CloseHandle(hFile);
}
public bool Lock()
{
uint bytesReturned = 0;
int retry = 0;
while (retry < 20)
{
if (Win32.DeviceIoControl(hFile, Win32.FSCTL_LOCK_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0,
ref bytesReturned, IntPtr.Zero))
{
return true;
}
System.Threading.Thread.Sleep(250);
retry++;
}
return false;
}
public bool Dismount()
{
uint bytesReturned = 0;
if (!Win32.DeviceIoControl(hFile, Win32.FSCTL_DISMOUNT_VOLUME, IntPtr.Zero, 0, IntPtr.Zero, 0,
ref bytesReturned, IntPtr.Zero))
{
return false;
}
return true;
}
public bool PreventRemoval(bool prevent)
{
Win32.PREVENT_MEDIA_REMOVAL pmr = new Win32.PREVENT_MEDIA_REMOVAL();
pmr.PreventMediaRemoval = prevent;
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(pmr));
Marshal.StructureToPtr(pmr, ptr, true);
uint bytesReturned = 0;
if (!Win32.DeviceIoControl(hFile, Win32.IOCTL_STORAGE_MEDIA_REMOVAL, ptr, Marshal.SizeOf(pmr), IntPtr.Zero, 0,
ref bytesReturned, IntPtr.Zero))
{
return false;
}
return true;
}
public bool AutoEject()
{
uint bytesReturned = 0;
if (!Win32.DeviceIoControl(hFile, Win32.IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0,
ref bytesReturned, IntPtr.Zero))
{
return false;
}
return true;
}
}
public enum EjectState
{
Failed,
NoVolume,
NoLock,
RemoveSafely,
AutoEject
}
public class Win32
{
public const uint FILE_DEVICE_FILE_SYSTEM = 0x00000009;
public const uint IOCTL_STORAGE_BASE = 0x0000002d;
public const uint FILE_ANY_ACCESS = 0;
public const uint FILE_READ_ACCESS = 1;
public const uint METHOD_BUFFERED = 0;
public const uint FSCTL_LOCK_VOLUME = ((FILE_DEVICE_FILE_SYSTEM << 16) | (FILE_ANY_ACCESS << 14) | (6 << 2) | (METHOD_BUFFERED));
public const uint FSCTL_DISMOUNT_VOLUME = ((FILE_DEVICE_FILE_SYSTEM << 16) | (FILE_ANY_ACCESS << 14) | (8 << 2) | (METHOD_BUFFERED));
public const uint IOCTL_STORAGE_MEDIA_REMOVAL = ((IOCTL_STORAGE_BASE << 16) | (FILE_READ_ACCESS << 14) | (0x0201 << 2) | (METHOD_BUFFERED));
public const uint IOCTL_STORAGE_EJECT_MEDIA = ((IOCTL_STORAGE_BASE << 16) | (FILE_READ_ACCESS << 14) | (0x0202 << 2) | (METHOD_BUFFERED));
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize,
IntPtr lpOutBuffer, uint nOutBufferSize, ref uint lpBytesReturned, IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hObject);
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const uint OPEN_EXISTING = 3;
public const int INVALID_HANDLE = -1;
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);
public struct PREVENT_MEDIA_REMOVAL
{
[MarshalAs(UnmanagedType.Bool)]
public bool PreventMediaRemoval;
}
}
To disable CD-ROM eject:
string DriveLetter = "G"; // Or whatever letter you have assigned for your CD-ROM
var VolumeManager = new VolumeManager(DriveLetter);
if (VolumeManager.OpenHandle())
{
if (!VolumeManager.PreventRemoval(true))
{
// Failed to disable CD-ROM eject
VolumeManager.CloseHandle();
}
}
else
{
// Failed to access volume
}
I Have fund a solution
#region EjectMedia
const uint GENERICREAD = 0x80000000;
const uint OPENEXISTING = 3;
const uint IOCTL_STORAGE_EJECT_MEDIA = 2967560;
const int INVALID_HANDLE = -1;
private static IntPtr fileHandle;
private static uint returnedBytes;
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr CreateFile(string fileName,
uint desiredAccess,
uint shareMode,
IntPtr attributes,
uint creationDisposition,
uint flagsAndAttributes,
IntPtr templateFile);
[DllImport("kernel32", SetLastError = true)]
static extern int CloseHandle(IntPtr driveHandle);
[DllImport("kernel32", SetLastError = true)]
static extern bool DeviceIoControl(IntPtr driveHandle,
uint IoControlCode,
IntPtr lpInBuffer,
uint inBufferSize,
IntPtr lpOutBuffer,
uint outBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);
#endregion
private void loop(object sender, EventArgs e)
{
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady)
{
if (d.DriveType == DriveType.Removable || d.DriveType == DriveType.Ram || d.DriveType == DriveType.Unknown)
Eject(#"\\.\" + d.Name.Substring(0, 1) + ":");
}
}
}
public void Eject(string driveLetter)
{
try
{
fileHandle = CreateFile(driveLetter, GENERICREAD, 0, IntPtr.Zero, OPENEXISTING, 0, IntPtr.Zero);
if ((int)fileHandle != INVALID_HANDLE)
DeviceIoControl(fileHandle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref returnedBytes, IntPtr.Zero);
}
catch
{
EventLog.WriteEntry(Marshal.GetLastWin32Error().ToString());
}
finally
{
CloseHandle(fileHandle);
fileHandle = IntPtr.Zero;
}
}

Resources