Re: RtlAdjustPrivilege and NtRaiseHardError

2020-05-22 Thread Mike Parker via Digitalmars-d-learn

On Friday, 22 May 2020 at 19:19:19 UTC, Arsium wrote:
Just I tried to launch those functions from win32 api and seems 
doesn't work




"doesn't work" isn't very helpful. Are you seeing compiler 
errors? Linker errors? Runtime errors? Please describe your 
problem.


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/22/20 5:39 PM, Vinod K Chandran wrote:

On Friday, 22 May 2020 at 20:51:20 UTC, Steven Schveighoffer wrote:

On 5/22/20 4:04 PM, Vinod K Chandran wrote:

[...]


Yes. What you cannot do is this (which I hope doesn't compile in 
VB.net, but I wouldn't be surprised):


Dim sampleList As New List(Of Child)
sampleList.Add(New Base(10))

Which is the equivalent of what you were requesting.


Nope--
List(Of Base) will contain an instance of a Child.
So in the same manner, i want
void function(Base) = fnPtr wiil work with
void function(Child)



That is the opposite of what you are thinking. A function pointer has to 
be valid based on its parameter types. Covariant functions are allowed.


This is OK:

void function(Child) fptr;

void foo(Base) {}

fptr =  // OK! it's fine to call fptr with a Child, because it is a 
Base as well


void function(Base) fptr2;

void foo2(Child) {}

fptr2 =  // Error! if you called fptr2 with a Base that is NOT a 
Child, bad things will happen.


This is more clear if you actually try calling them:

fptr2(new Base); // the compiler should allow this
foo2(new Base); // but would not allow this

So why should fptr2 be allowed to point at foo2?

-Steve


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, May 22, 2020 at 09:39:16PM +, Vinod K Chandran via 
Digitalmars-d-learn wrote:
[...]
> So in the same manner, i want
> void function(Base) = fnPtr wiil work with
> void function(Child)

You cannot, because that's type unsafe:

class Base {}
class Derived : Base {
void derivedFunc() {}
}
class Another : Base {}

void derivedFunc(Derived d) { d.derivedFunc(); }

void function(Base) funPtr;
funPtr = derivedFunc; // suppose this was allowed

Base obj = new Another;
funPtr(obj); // crash: obj does not have derivedFunc()


T

-- 
Век живи - век учись. А дураком помрёшь.


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread Vinod K Chandran via Digitalmars-d-learn
On Friday, 22 May 2020 at 20:51:20 UTC, Steven Schveighoffer 
wrote:

On 5/22/20 4:04 PM, Vinod K Chandran wrote:

[...]


Yes. What you cannot do is this (which I hope doesn't compile 
in VB.net, but I wouldn't be surprised):


Dim sampleList As New List(Of Child)
sampleList.Add(New Base(10))

Which is the equivalent of what you were requesting.

-Steve

Nope--
List(Of Base) will contain an instance of a Child.
So in the same manner, i want
void function(Base) = fnPtr wiil work with
void function(Child)



Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, May 22, 2020 at 08:55:45PM +, Vinod K Chandran via 
Digitalmars-d-learn wrote:
> On Friday, 22 May 2020 at 20:06:20 UTC, Adam D. Ruppe wrote:
> > On Friday, 22 May 2020 at 20:04:24 UTC, Vinod K Chandran wrote:
> > > sampleList.Add(New Child(10.5)) Is this possible in D without
> > > casting ?
> > 
> > Direct translation of this code works just fine in D.
> 
> Yeah, my bad. I just checked in D. But think inherited type difference
> is a problem in function pointer's parameters only.
> alias EvtFuncPtr = void function(EventArgs);
> Now, this EvtFuncPtr won't allow any derived classes of EventArgs as
> parameter. That's the problem i am facing.
[...]

So you're basically saying:

void function(DerivedClass)

cannot implicitly convert to:

void function(BaseClass)

right?

This is as it should be:

class Base { ... }
class Derived : Base { ... }
class Another : Base { ... }

void baseFunc(Base) { ... }
void derivedFunc(Derived) { ... }

void function(Base) funcPtr;
funcPtr = baseFunc; // OK
funcPtr = derivedFunc;  // Not allowed

Why is it bad to assign derivedFunc to funcPtr?  Consider this:

Base obj = new Another;
funcPtr = baseFunc;
funcPtr(obj);   // OK, because Another is a subtype of Base

funcPtr = derivedFunc; // suppose this was allowed
funcPtr(obj);   // Uh oh, derivedFunc receives an argument of
// type Another which is not a subtype of
// Derived

So, it's not permissible to allow assigning derivedFunc to funcPtr
without a cast.


T

-- 
Indifference will certainly be the downfall of mankind, but who cares? -- 
Miquel van Smoorenburg


Re: to but nothrow?

2020-05-22 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, May 22, 2020 at 08:25:24PM +, bauss via Digitalmars-d-learn wrote:
> Is there anyway to use the "to" template from std.conv but as nothrow?
[...]

There's std.conv.parse, though the interface is somewhat awkward, and it
only works with character ranges.


T

-- 
I'm still trying to find a pun for "punishment"...


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread Vinod K Chandran via Digitalmars-d-learn

On Friday, 22 May 2020 at 20:06:20 UTC, Adam D. Ruppe wrote:

On Friday, 22 May 2020 at 20:04:24 UTC, Vinod K Chandran wrote:
sampleList.Add(New Child(10.5)) Is this possible in D without 
casting ?


Direct translation of this code works just fine in D.


Yeah, my bad. I just checked in D. But think inherited type 
difference is a problem in function pointer's parameters only.

alias EvtFuncPtr = void function(EventArgs);
Now, this EvtFuncPtr won't allow any derived classes of EventArgs 
as parameter. That's the problem i am facing. What about a 
template ?


alias EvtFuncPtr = void function(T)(T = EventArgs);
This is not compiled.


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/22/20 4:04 PM, Vinod K Chandran wrote:

On Friday, 22 May 2020 at 16:12:12 UTC, Steven Schveighoffer wrote:

On 5/22/20 9:10 AM, Vinod K Chandran wrote:

On Friday, 22 May 2020 at 12:21:25 UTC, rikki cattermole wrote:

if (Child child = cast(Child)parent) {
assert(child !is null);
}


Actually, problem occurs in addHandler function. It expects an 
argument of type "EventArgs", not MouseEventArgs.


Yes, because what if you did this with your function:

fnp(new EventArgs(...));

It would be called with the type being implicitly cast to the child 
type without that being true.


What Rikki was recommending is that you write your handler like this:

void onClick(EventArgs e){
    if(auto me = cast(MouseEventArgs)e) {
    log("form clicked on x = ", me.x, ", and y = ", me.y);
    }
}

Actually, if you are certain it's a programming error for onClick to 
be called with a different type of event args, I'd do:


void onClick(EventArgs e){
    auto me = cast(MouseEventArgs)e;
    assert(me !is null, "Error, onClick called with invalid event type");
    log("form clicked on x = ", me.x, ", and y = ", me.y);
}


Thanks for the answer. I understand that, in D, derived class and base 
class are not the same as in vb.net or any other language. (Please 
correct me if i am wrong).

In vb.net, assume that i have a class setup like this--
Public Class Base
     Public Property SampleInt As Integer
End Class

Public Class Child : Inherits Base
     Public Property SampleDouble As Double
End Class
//Assume that i have a list of Base class like this--
Dim sampleList As New List(Of Base)
// Now, i can use this list like this--
sampleList.Add(New Child(10.5)) Is this possible in D without casting ?



Yes. What you cannot do is this (which I hope doesn't compile in VB.net, 
but I wouldn't be surprised):


Dim sampleList As New List(Of Child)
sampleList.Add(New Base(10))

Which is the equivalent of what you were requesting.

-Steve


to but nothrow?

2020-05-22 Thread bauss via Digitalmars-d-learn
Is there anyway to use the "to" template from std.conv but as 
nothrow?


Having it throw exceptions is not always acceptable because it's 
generally expensive.


Something that attempted to convert would be far more fesible.

Is there anything like that?

Ex. from C# there's int.TryParse etc.

Those all return booleans for whether the conversion was 
successful or not.


Like what would the equivalent of this be:

```
if (int.TryParse("1234", out int number))
{
// Use number ...
}
```

Sure I can do:

```
bool tryParse(From,To)(From fromValue, out To toValue)
{
try
{
toValue = fromValue.to!To;
return true;
}
catch (ConvException e)
{
toValue = To.init;
return false;
}
}
```

But that does not seem like an efficient approach.


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread Adam D. Ruppe via Digitalmars-d-learn

On Friday, 22 May 2020 at 20:04:24 UTC, Vinod K Chandran wrote:
sampleList.Add(New Child(10.5)) Is this possible in D without 
casting ?


Direct translation of this code works just fine in D.


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread Vinod K Chandran via Digitalmars-d-learn
On Friday, 22 May 2020 at 16:12:12 UTC, Steven Schveighoffer 
wrote:

On 5/22/20 9:10 AM, Vinod K Chandran wrote:

On Friday, 22 May 2020 at 12:21:25 UTC, rikki cattermole wrote:

if (Child child = cast(Child)parent) {
assert(child !is null);
}


Actually, problem occurs in addHandler function. It expects an 
argument of type "EventArgs", not MouseEventArgs.


Yes, because what if you did this with your function:

fnp(new EventArgs(...));

It would be called with the type being implicitly cast to the 
child type without that being true.


What Rikki was recommending is that you write your handler like 
this:


void onClick(EventArgs e){
if(auto me = cast(MouseEventArgs)e) {
log("form clicked on x = ", me.x, ", and y = ", me.y);
}
}

Actually, if you are certain it's a programming error for 
onClick to be called with a different type of event args, I'd 
do:


void onClick(EventArgs e){
auto me = cast(MouseEventArgs)e;
assert(me !is null, "Error, onClick called with invalid 
event type");

log("form clicked on x = ", me.x, ", and y = ", me.y);
}


Thanks for the answer. I understand that, in D, derived class and 
base class are not the same as in vb.net or any other language. 
(Please correct me if i am wrong).

In vb.net, assume that i have a class setup like this--
Public Class Base
Public Property SampleInt As Integer
End Class

Public Class Child : Inherits Base
Public Property SampleDouble As Double
End Class
//Assume that i have a list of Base class like this--
Dim sampleList As New List(Of Base)
// Now, i can use this list like this--
sampleList.Add(New Child(10.5)) Is this possible in D without 
casting ?




RtlAdjustPrivilege and NtRaiseHardError

2020-05-22 Thread Arsium via Digitalmars-d-learn
Just I tried to launch those functions from win32 api and seems 
doesn't work


Here is my code

module D_Programming_Test;

import core.sys.windows.windows;


pragma(lib, "ntdll.lib");

alias extern(C) int function(string[] args) MainFunc;
extern (C) int _d_run_main(int argc, char **argv, MainFunc 
mainFunc);


extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR 
lpCmdLine, int nCmdShow)

{
return _d_run_main(0, null, ); // arguments unused, 
retrieved via CommandLineToArgvW

}

extern (Windows)
LRESULT NtRaiseHardError(int az512 , uint az56 , uint sqd56 , int 
sqd52  , int foazeo ,   uint azd);



extern (Windows)
int RtlAdjustPrivilege(ulong az512 , bool az56 , bool sqd56 , 
bool sqd52 )

{
return 1;
}

extern(C) int main(string[] args)
{
MessageBoxW(null, "Hello D World!"w.ptr, "D Windows 
Application"w.ptr, MB_OK);


bool t1 ;
uint t2  ;
int i  = 0xC022 ;

RtlAdjustPrivilege(19, true, false, t1);

NtRaiseHardError(i, 0, 0, 0, 6, t2);


return 0;
}






Re: How to allocate/free memory under @nogc

2020-05-22 Thread welkam via Digitalmars-d-learn

There is automem to help with manual memory management
https://code.dlang.org/packages/automem


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/22/20 9:10 AM, Vinod K Chandran wrote:

On Friday, 22 May 2020 at 12:21:25 UTC, rikki cattermole wrote:

if (Child child = cast(Child)parent) {
assert(child !is null);
}


Actually, problem occurs in addHandler function. It expects an argument 
of type "EventArgs", not MouseEventArgs.


Yes, because what if you did this with your function:

fnp(new EventArgs(...));

It would be called with the type being implicitly cast to the child type 
without that being true.


What Rikki was recommending is that you write your handler like this:

void onClick(EventArgs e){
if(auto me = cast(MouseEventArgs)e) {
log("form clicked on x = ", me.x, ", and y = ", me.y);
}
}

Actually, if you are certain it's a programming error for onClick to be 
called with a different type of event args, I'd do:


void onClick(EventArgs e){
auto me = cast(MouseEventArgs)e;
assert(me !is null, "Error, onClick called with invalid event type");
log("form clicked on x = ", me.x, ", and y = ", me.y);
}


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread Vinod K Chandran via Digitalmars-d-learn

On Friday, 22 May 2020 at 12:21:25 UTC, rikki cattermole wrote:

if (Child child = cast(Child)parent) {
assert(child !is null);
}


Actually, problem occurs in addHandler function. It expects an 
argument of type "EventArgs", not MouseEventArgs.


Re: How to use base class & child class as parameter in one function ?

2020-05-22 Thread rikki cattermole via Digitalmars-d-learn

if (Child child = cast(Child)parent) {
assert(child !is null);
}


How to use base class & child class as parameter in one function ?

2020-05-22 Thread Vinod K Chandran via Digitalmars-d-learn




Hi all,
I have a windows gui setup like this;

class EventArgs {} \\ Base class for all messages
class MouseEventArgs : EventArgs {	// child class for handling 
mouse messages

...
int x;
int y;
this(WPARAM wpm, LPARAM lpm){
this.x = xFromLparam(lpm);
this.y = yFromLparam(lpm);
}
}

alias EvtFuncPtr = void function(EventArgs); // function pointer

struct MsgHandler {
uint message ;
HWND handle ;
bool isActive ;
EvtFuncPtr fnPtr;
}
// Then in my window class...
void addHandler(uint we, EvtFuncPtr fnp){ // This is error point.
auto mh = MsgHandler();
mh.handle = this.mHandle;
mh.message = we;
mh.fnPtr = fnp;
mh.isActive = true;
	this.msgHandlerList ~= mh;  // This is a list in 
window class.	

}

// And in the WndProc...
auto thisWin = findWindowClass(hWnd);  // get the window class 
with hWnd.
auto mh = thisWin.findHandler(hWnd, message);// get the event 
handler for this message & hWnd

if(mh.isActive) { // if there is an event handler fixed,
switch (message){
   case 512 : .. case 526 :  // if it's a Mouse messages
auto ea = new MouseEventArgs(wParam, lParam);
mh.fnPtr(ea);   // execute that event handler function
break;
   default : break;
}
}

// And this is the window creation site...
auto app = new Application() ;
auto frm = new Window(app) ;
frm.createWindow() ;

frm.addHandler(frm.load, );
frm.addHandler(frm.Click, );

void onLoad(EventArgs e){
log("form is loaded...");
}
void onClick(MouseEventArgs e){	 // Compiler wants to change the 
MouseEventArgs with EventArgs.

log("form clicked on x = ", e.x, ", and y = ", e.y);
}

I wrote  EventArgs as the parameter type in function pointer but 
i want to use it's child classes also. But i can't.




Re: Fibers and std.socket

2020-05-22 Thread ikod via Digitalmars-d-learn

On Friday, 22 May 2020 at 06:10:38 UTC, Atwork wrote:

Is it possible to mix fibers with sockets from phobos?

If so, how would I do it?

Like just a simple example of async sockets using fibers in D.

I will say that I'd prefer to not use any packages ex. vibe.d


Yes you can mix std sockets with fibers, but then you have two 
options - either sockets are in blocking mode (and every socket 
can block current thread and other fibers) or you use sockets in 
non-blocking mode and then you have to manage socket events in 
some hand-made event loop. AFAIK there is no other options.


Re: How to allocate/free memory under @nogc

2020-05-22 Thread Kagamin via Digitalmars-d-learn

On Thursday, 21 May 2020 at 17:19:10 UTC, Konstantin wrote:
Hi all! I will try to ask again(previous two posts still have 
no answers) : are there any site/page/docs somewhere to track 
actual info about @nogc support in language itself and in 
phobos library? Or, at least plans to extend such support?


https://dlang.org/spec/function.html#nogc-functions - here's 
language support. Phobos tends to infer attributes like @nogc, so 
it's context dependent, the actual info is provided by the 
compiler.


Fibers and std.socket

2020-05-22 Thread Atwork via Digitalmars-d-learn

Is it possible to mix fibers with sockets from phobos?

If so, how would I do it?

Like just a simple example of async sockets using fibers in D.

I will say that I'd prefer to not use any packages ex. vibe.d