July 14, 2014
Yes yes, I did it, I used the anonymous type

Look the complete code: https://gist.github.com/bencz/3576dfc8a217a34c05a9

I know, has several things that can be improved
July 14, 2014
On Monday, 14 July 2014 at 14:50:36 UTC, Alexandre wrote:
> Yes yes, I did it, I used the anonymous type
>
> Look the complete code: https://gist.github.com/bencz/3576dfc8a217a34c05a9
>
> I know, has several things that can be improved

Now that you've done that, can you build us a linker that reads COFF libs so we can lose optlink:)
July 14, 2014
Soory, I not understand, why lose the optlink ?
Read the libs is simple, but, the linker do the brute force!

On Monday, 14 July 2014 at 15:17:06 UTC, Jason King wrote:
> On Monday, 14 July 2014 at 14:50:36 UTC, Alexandre wrote:
>> Yes yes, I did it, I used the anonymous type
>>
>> Look the complete code: https://gist.github.com/bencz/3576dfc8a217a34c05a9
>>
>> I know, has several things that can be improved
>
> Now that you've done that, can you build us a linker that reads COFF libs so we can lose optlink:)

July 14, 2014
Alexandre:

> Look the complete code: https://gist.github.com/bencz/3576dfc8a217a34c05a9
>
> I know, has several things that can be improved

memcpy(&dosh.e_magic, "MZ".ptr, 2);
memcpy(&peh.Signature, "PE\0\0".ptr, 4);
memcpy(scth[1].Name.ptr, ".idata".ptr, 6);
memcpy(scth[2].Name.ptr, ".data".ptr, 5);
memcpy(&image[0x428], x"3820".ptr, 2);
memcpy(&image[0x430], x"3820".ptr, 2);
memcpy(&image[0x43a], "printf".ptr, 6);
memcpy(&image[0x448], "msvcrt.dll".ptr, 10);
memcpy(&image[0x201], x"00304000".ptr, 4);
memcpy(&image[0x207], x"30204000".ptr, 4);
memcpy(&image[0x600], "hello\n".ptr, 6);

Instead of this very bug-prone code, write yourself a little function to perform this more safely.

Bye,
bearophile
July 14, 2014
void InjectData(T)(ref T BaseSrc, string data)
{
	memcpy(&BaseSrc, data.ptr, data.length);
}

It's possible to improve this function ?

On Monday, 14 July 2014 at 15:45:19 UTC, bearophile wrote:
> Alexandre:
>
>> Look the complete code: https://gist.github.com/bencz/3576dfc8a217a34c05a9
>>
>> I know, has several things that can be improved
>
> memcpy(&dosh.e_magic, "MZ".ptr, 2);
> memcpy(&peh.Signature, "PE\0\0".ptr, 4);
> memcpy(scth[1].Name.ptr, ".idata".ptr, 6);
> memcpy(scth[2].Name.ptr, ".data".ptr, 5);
> memcpy(&image[0x428], x"3820".ptr, 2);
> memcpy(&image[0x430], x"3820".ptr, 2);
> memcpy(&image[0x43a], "printf".ptr, 6);
> memcpy(&image[0x448], "msvcrt.dll".ptr, 10);
> memcpy(&image[0x201], x"00304000".ptr, 4);
> memcpy(&image[0x207], x"30204000".ptr, 4);
> memcpy(&image[0x600], "hello\n".ptr, 6);
>
> Instead of this very bug-prone code, write yourself a little function to perform this more safely.
>
> Bye,
> bearophile

July 14, 2014
Alexandre:

> void InjectData(T)(ref T BaseSrc, string data)
> {
> 	memcpy(&BaseSrc, data.ptr, data.length);
> }
>
> It's possible to improve this function ?

You can add some modifiers (like @nogc for dmd 2.066), and the name of D functions starts with a lower case.

Bye,
bearophile
July 15, 2014
I have this struct:

enum AddrType { Abs, RVA, Rel };
struct Address
{
    shared_ptr<DWORD> addr;
    AddrType type;

    Address(): type(Abs) {}
    Address(DWORD addr, AddrType type):
        addr(shared_ptr<DWORD>(new DWORD(addr))), type(type) {}
    Address(shared_ptr<DWORD> addr, AddrType type):
        addr(addr), type(type) {}
    Address(const Address &ad):
        addr(ad.addr), type(ad.type) {}
};

My problem is with default alue init...
Address(): type(Abs) {}
Address(DWORD addr, AddrType type):
        addr(shared_ptr<DWORD>(new DWORD(addr))), type(type) {}

How to make this in D ?
I do this:

struct Address
{
	RefCounted!(DWORD) addr;
	AddrType type = AddrType.Abs;
	this(DWORD addr, AddrType type)
	{
		addr(RefCounted!(DWORD)(new DWORD(addr)));
		this.type = type;
	}
	/*Address(shared_ptr<DWORD> addr, AddrType type) :
	addr(addr), type(type) {}
	Address(const Address &ad) :
	addr(ad.addr), type(ad.type) {}*/
};

It's correct ?


In case for a template, when I have this:
template <typename T> struct Wrap {
    T val;
    Wrap(T val): val(val) {}
};

I maded this:
template Wrap(T)
{
	struct Wrap
	{
		T val;
		this(T val){val = val;}
	}
}

PS: ( I know about identation... it's wrong here... )
July 15, 2014
Alexandre:

> 	RefCounted!(DWORD) addr;

I think RefCounted is for advanced usages in D :-)


> template Wrap(T)
> {
> 	struct Wrap
> 	{
> 		T val;
> 		this(T val){val = val;}
> 	}
> }

Simpler:

struct Wrap(T) {
    T val;
    this(T val_) { this.val = val_; }
}


Or just:

struct Wrap(T) { T val; }

Bye,
bearophile
July 15, 2014
Oh!
I used the RefCounted because this:
"The proposed C++ shared_ptr<>, which implements ref counting, suffers from all these faults. I haven't seen a heads up benchmark of shared_ptr<> vs mark/sweep, but I wouldn't be surprised if shared_ptr<> turned out to be a significant loser in terms of both performance and memory consumption.

That said, D may in the future optionally support some form of ref counting, as rc is better for managing scarce resources like file handles. Furthermore, if ref counting is a must, Phobos has the std.typecons.RefCounted type which implements it as a library, similar to C++'s shared_ptr<>."

I found that in this link: http://dlang.org/faq.html#reference-counting

On Tuesday, 15 July 2014 at 14:46:01 UTC, bearophile wrote:
> Alexandre:
>
>> 	RefCounted!(DWORD) addr;
>
> I think RefCounted is for advanced usages in D :-)
>
>
>> template Wrap(T)
>> {
>> 	struct Wrap
>> 	{
>> 		T val;
>> 		this(T val){val = val;}
>> 	}
>> }
>
> Simpler:
>
> struct Wrap(T) {
>     T val;
>     this(T val_) { this.val = val_; }
> }
>
>
> Or just:
>
> struct Wrap(T) { T val; }
>
> Bye,
> bearophile

July 15, 2014
Alexandre:

> as rc is better for managing scarce resources like file handles.

File instances are ref counted in D. Is this useful for you?

Bye,
bearophile