I create hidden IWebBrowser2 object and it work's fine but after few seconds I want to
change visibility to true and my application crashes.
pBrowser2->put_Visible(VARIANT_TRUE);
What I'm doing wrong ?
You're using pBrowser2 after releasing it.
You have:
pBrowser2->Release(); // pBrowser NOW INVALID!!!
srand( time( NULL ) );
//Sleep( ( std::rand() % 5000 ) + 5000 );
if(std::rand() % 100 <= chance ){
pBrowser2->put_Visible(VARIANT_TRUE); // instant crash here!
}
Move the call to Release() after you're done using it, or use a COM smart pointer so you don't have to manage it yourself. Simplest fix:
srand( time( NULL ) );
//Sleep( ( std::rand() % 5000 ) + 5000 );
if(std::rand() % 100 <= chance ){
pBrowser2->put_Visible(VARIANT_TRUE); // works
}
pBrowser2->Release(); // pBrowser NOW INVALID!!!
Related
I am trying to write Windows driver code to scan a directory for file names:
HANDLE directory_handle;
FILE_BOTH_DIR_INFORMATION directory_information;
IO_STATUS_BLOCK io_status_block;
NTSTATUS status;
OBJECT_ATTRIBUTES directory_attributes;
InitializeObjectAttributes(&directory_attributes ,
&directory_name ,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
0 ,
0 );
status = ZwCreateFile(&directory_handle ,
FILE_LIST_DIRECTORY | SYNCHRONIZE ,
&directory_attributes ,
&io_status_block ,
0 ,
0 ,
FILE_SHARE_VALID_FLAGS ,
FILE_OPEN ,
FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE,
0 ,
0 );
status = ZwQueryDirectoryFile(directory_handle ,
NULL ,
0 ,
0 ,
&io_status_block ,
&directory_information ,
sizeof(directory_information),
FileBothDirectoryInformation ,
TRUE ,
NULL ,
FALSE );
status = ZwQueryDirectoryFile(directory_handle ,
NULL ,
0 ,
0 ,
&io_status_block ,
&directory_information ,
sizeof(directory_information),
FileBothDirectoryInformation ,
TRUE ,
NULL ,
FALSE );
The first call to ZwQueryDirectoryFile() returns a STATUS_SUCCESS (0x00000000) result, but the second call returns a status of STATUS_BUFFER_OVERFLOW (0x80000005). Do you know what might cause this buffer-overflow error?
Thank you.
STATUS_BUFFER_OVERFLOW indicates that the buffer isn't big enough to return the full filename (however we know the buffer is big enough for at least the base structure, else STATUS_BUFFER_TOO_SMALL).
What's happening here is the first call successfully returned the "." entry (as there is room in the base structure for a single character filename), but the second call failed with STATUS_BUFFER_OVERFLOW because the buffer isn't big enough for the ".." entry.
FILE_ID_BOTH_DIR_INFORMATION is a variable-length structure.
You will need to allocate suffucient space to receive the path string at the end of the structure.
You will need to pass the size of the allocated buffer to ZwQueryDirectoryFile rather than sizeof(FILE_ID_BOTH_DIR_INFORMATION)
No idea why the first one works.
I found many examples of how to reset timer, but they usually concerned manual reset (e.g. on-click button event).
I need a logic that will automatically reset the value when the countdown ends.
Timer:
type seconds = number;
const getRemainingTime$ = (store: Store): Observable<seconds> => {
// calculate fullTime based on the TriggerDate extracted from the State
// ...
return fullTime$.pipe(
switchMap((fullTime: seconds) =>
timer(0, 1000).pipe(
map((tickCount: number) => fullTime - tickCount),
takeWhile((remainingTime: seconds) => remainingTime >= 0)
)
)
);
}
Trigger (wait for 0 value on timer)
getRemainingTime$(this.store).pipe(
skipWhile((remainingTime: seconds) => remainingTime > 0),
)
.subscribe(data => {
const newTriggerDate: Date = new Date(new Date().getTime() + 60 * 1000); // +60 seconds
this.store.dispatch([new SetTriggerDateAction({ newTriggerDate })]);
});
...and it doesn't work -
When the remaining time is zero, the trigger goes crazy and dispatch an infinite number of actions. What is wrong?
PS: When I manually dispatch SetTriggerDateAction (onClick button), the problem disappears.
It was enough to replace skipWhile to a filter.
skipWhile
All values are emitted if the condition in skipWhile is met at least once.
filter
Only those values specified in the condition are emitted.
I have a HashMap :
HashMap<string, Integer> hmap = new HashMap<>();
where I want to increase the HashMap value. In order to avoid the nullPointer Exception if the key doesn't exist, I check it! Let's say the data are:
//201803271 - 1000
//201803271 - 1000
//201803272 - 1000
//inside a loop i read the data...
if (hmap.get("201803271") != null) {
hmap.put("201803271", hmap.get("201803271") + 1000);
}else{
hmap.put("201803271", 1000);
}
//end of loop
which works as I get:
201803271 - 2000
201803272 - 1000
But, I read this question How to update a value, given a key in a java hashmap? and there is a solution to use the Java 8 method getOrDefault. I tried it
hmap.put("201803271", count.getOrDefault("201803271", 1000) + 1000)
However, with this solution I get wrong results...
201803271 - 3000
201803272 - 2000
What am I missing?
Java 8 introduced merge method to Map interface just for this type of problem:
hmap.merge("201803271", 1000, Integer::sum);
It means "put 1000 for this key but if this key already has a value add 1000 to it".
The reason your solution wasn't working is that you were getting 1000 by default and then adding 1000 to it. To do this correctly with getOrDefault, you would want to replace 1000 with 0 in getOrDefault. hmap.put("201803271", count.getOrDefault("201803271", 0) + 1000))
You could do it like this:
map.put(key, map.getOrDefault(key, 0) + inc);
or
map.compute(key, (k, v) -> v == null ? inc : v + inc);
Just working through the samples, and got the exercise about creating 2 random dice and rolling them with a button.
http://guide.elm-lang.org/architecture/effects/random.html
So I thought I would create the dice as a module, remove the roll action, and just have it create a D6 value on init.
So my code is now as follows (should open direct in elm-reactor)
module Components.DiceRoller exposing (Model, Msg, init, update, view)
import Html exposing (..)
import Html.App as Html
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Random
import String exposing (..)
main =
Html.program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
-- MODEL
type alias Model =
{ dieFace : Int
}
init : ( Model, Cmd Msg )
init =
( Model 0, (Random.generate NewFace (Random.int 1 6)) )
-- UPDATE
type Msg
= NewFace Int
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NewFace newFace ->
( Model newFace, Cmd.none )
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
dieFaceImage : Int -> String
dieFaceImage dieFace =
concat [ "/src/img/40px-Dice-", (toString dieFace), ".svg.png" ]
view : Model -> Html Msg
view model =
let
imagePath =
dieFaceImage model.dieFace
in
div []
[ img [ src imagePath ] []
, span [] [ text imagePath ]
]
The problem with this is that it is always producing the same value. I thought I had a problem with the seed to begin with, but if you change
init =
( Model 0, (Random.generate NewFace (Random.int 1 6)) )
init =
( Model 0, (Random.generate NewFace (Random.int 1 100)) )
it works exactly as intended. So it looks like the default generator is not working with small values, seems to work as low down as 10.
The odd thing is this, in this example (which i started with) http://guide.elm-lang.org/architecture/effects/random.html , it works fine with 1-6 when it's not in init.
So my question is, am I doing something wrong, or is this just a wrinkle in elm? Is my usage of the command in init ok?
In the end, I put this in to get the desired effect, which feels wonky.
init =
( Model 0, (Random.generate NewFace (Random.int 10 70)) )
with
NewFace newFace ->
( Model (newFace // 10), Cmd.none )
This must to have something to do with seeding. You're not specifying any value for seed so the generator is defaulting to use the current time.
I think you tried to refresh your page for a few times in a few seconds and you didn't see the value change. If you wait for longer (roughly a minute) you'll see your value change.
I had a look at the source code of Random and I suspect that for seed values that are close enough the first value generated in the range [1,6] doesn't change. I'm not sure whether this is expected or not, probably it's worth raising an issue on GitHub
guys. I created this procedure in NetLogo for my agents (farmers):
to calculate-deforestation
ask farmers [
set net-family-labor ( family-labor - ( ag-size * cell-labor-ag-keep ) )
set net-family-money ( family-money - ( ag-size * cell-cost-ag-keep ) )
ifelse net-family-labor < 0 or net-family-money < 0
[ set n-aband-cell-labor ( family-labor / cell-labor-ag-keep )
set n-aband-cell-money ( family-money / cell-cost-ag-keep )
set n-aband with-max [ n-aband-cell-labor n-aband-cell-money ]
]
[ set n-def-cell-labor ( net-family-labor / cell-labor-deforest )
set n-def-cell-money ( net-family-money / cell-cost-deforest )
set n-def with-min [ n-def-cell-labor n-def-cell-money ]
]
]
end
For the "n-aband", I would like to get the max value between "n-aband-cell-labor" and "n-aband-cell-money" (either one or the other; the same goes for "n-def"). I know a limited number of NetLogo primitives but the ones I was able to find do not work for my case, for instance, "with-max", "max-n-of", "max-one-of". I am sure there must be one that would work but I am having trouble finding it in the NetLogo dictionary. I wonder if anyone could suggest me one that could work for my case. Thank you in advance.
If you want to get the max value of a list, simply use max. So,
set n-aband max (list n-aband-cell-labor n-aband-cell-money )
will set n-aband to the highest of the two values.